dimanche 19 janvier 2020

Iterating through a string and comparing result to another string using .equals (Java)

import java.util.Scanner;

public class Main {

    public static void main(String[] args){

        Landscape landscape = new Landscape();

        Scanner keyboardScanner = new Scanner(System.in);

        System.out.println("Enter your terrain: ");

        String TerrainInput = keyboardScanner.nextLine();

        for (int i = 0; i < TerrainInput.length(); i++){

            char j = TerrainInput.charAt(i);

            if j.**equals(**"m") {
                //landscape.mountain(int [j+1])
            }

            if j.**equals**("f"){
                //landscape.flat(int [j+1])
            }
            if j.**equals**("h"){
                //landscape.hill(int [j+1])
            }
        }

    landscape.print();

The program should receive some input string like f3,m3,f2,m0,f4,h1,f1 my plan is to iterate through each character and check if it is equal "f,m, or h" and if it is then i will call the appropriate method and use the number after "f,m, or h" as an argument by calling the method with the index of the letter character found + 1.

for (int i = 0; i < TerrainInput.length(); i++){
    char j = TerrainInput.charAt(i);

    if j.**equals(**"m") {
        //landscape.mountain(int [j+1])
    }
}

The issue is the .equals isn't working, its saying Cannot resolve method 'equals(java.lang.String)'

Aucun commentaire:

Enregistrer un commentaire