Ruairi Lavery
Ruairi Lavery

Reputation: 1

trying to compare user input with a text file and then read the values of numbers from that point on java

while(inFile.hasNext())
{
    String Desnation = inFile.next();

    if(givenLocation.equalsIgnoreCase(Desnation))
    {
        int num1 = inFile.nextLine();
        int num2 = inFile.nextLine();

        System.out.println("Yay!");
    }
}

It is saying that it can't convert a string to an int so tried to parse it before hand but the substring wasn't registering the input from user.

Upvotes: 0

Views: 71

Answers (1)

Keammoort
Keammoort

Reputation: 3075

You have to convert String into Integer:

try {
    int num1 = Integer.parseInt(inFile.nextLine());
    int num2 = Integer.parseInt(inFile.nextLine());
} catch(NumberFormatException e) {
    //handle exception, ex print some text
}

BTW as Java naming convention says:

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

consider changing Desnation to

desnation

Upvotes: 2

Related Questions