Zaid A Zaid A - 25 days ago 6
Java Question

Cannot find symbol for .hasNextInt

import java.util.*;

public class NGGs {

static int RandomNum1 = (int) (Math.random() * 11);

static int guessTyped;

static Scanner guessedNumber;

static String answer;

static Scanner reply;

public static void main(String[] args) {

guess(guessTyped, guessedNumber, RandomNum1);
}

public static void guess(int userGuess, Scanner userInput, int RandomNum1) {

do{

System.out.println("Im thinking of a Number between 1 to 10.....");

System.out.print("Try to guess it: ");

userInput = new Scanner(System.in);

userGuess = userInput.nextInt();

if (!userInput.hasNextInt) {

System.out.println("That wasn't the answer I was quite looking for, try typing in a number that doesn't have decimals!");

continue;

} else {

guessChecker(guessTyped, guessedNumber, RandomNum1);

}
} while (userGuess != RandomNum1);

}

public static void guessChecker(int userGuess, Scanner userInput, int RandomNum1) {

if (userGuess == RandomNum1) {

System.out.println("Congradulations!");

} else {

retryChecker(reply, answer);
}

}

public static void retryChecker(Scanner userReply, String userChoice) {

System.out.println("Would you like to try again ?");

userReply = new Scanner(System.in);

userChoice = userReply.nextLine();

if (userChoice.equalsIgnoreCase("yes") || userChoice.equalsIgnoreCase("y")) {

guess(guessTyped, guessedNumber, RandomNum1);

} else {

System.exit(0);
}

}

public static void delay(int millis) {

try {

Thread.sleep(millis);

} catch (InterruptedException exp) {}
}


}

ok so there's my code. (btw NGGs stands for NumberGuessingGame).
so im trying to say that if userInput is not an Integer then print out:
That wasn't the answer I was quite looking for, try typing in a number that doesn't have decimals!
but, it says cannot find symbol and i cannot solve the issue and it's bothering me alot.
Thanks for the help.

Answer

The problem is that in Java, we are invoking methods with using "(...)" with list of method parameters in "..." place. (in case of lambdas and method references there is the other way).
userInput.hasNextInt() is not o field, it's a method so this rule have to be applied.
So in your code, you should repleace:

if (!userInput.hasNextInt) {

with:

if (!userInput.hasNextInt()) {

Hope it helps.