0

I'm working on a homework problem of creating a guessing game. I've got that part working, but we have to validate the input. I've tried using hasNextInt, but I keep getting an error saying "int cannot be dereferenced" and points to the "!guess.hasNextInt" code.

I've tried many iterations, but I still get the same error. The code I'm including is just my most recent try.

How do I get hasNextInt to work or how else should I validate the input?

import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        int num = (int) (Math.random() * 101);

        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to my Guessing Game!");

        int guess = -1;
        //Loop goes as long as guess doesn't equal num
        while (guess != num) {
            System.out.print("Guess a number between 1 and 100:   ");
            guess = input.nextInt();

            //Validates input
            while (!guess.hasNextInt()) {
                System.out.println("Invalid response, try again.");
                in.next();
            }

            if (guess == num)
                System.out.println("Correct!");
            else if (guess < num)
                System.out.println("Your guess was too low");
            else
                System.out.println("Your guess was too high");

        }
    }
}
CJarbez
  • 3
  • 4
  • The variable `guess` is of the primitive type `int`. Therefore you can't call methods on it, such as when you try to call `guess.hasNextInt()`. `hasNextInt()` is a method of the `Scanner` class. Also note that `hasNextInt()` won't serve the purpose of validating the input. – JustAnotherDeveloper Sep 05 '20 at 15:13
  • `.hasNextInt()` is a method of `Scanner` class, you are trying to call it on an `int` variable. You probably meant to use `input` instead of `guess`: `input.hasNextInt()` – Yousaf Sep 05 '20 at 15:13

2 Answers2

1

i fixed the code:

public static void main(String[] args) {
    int num = (int) (Math.random() * 101);
    System.out.println(num);
    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to my Guessing Game!");

    int guess = -1;
    //Loop goes as long as guess doesn't equal num
    while (guess != num) {
        System.out.print("Guess a number between 1 and 100:   ");
        guess = input.nextInt();

        if (guess == num) {
            System.out.println("Correct!");
            break;}
        else if (guess < num)
            System.out.println("Your guess was too low");
        else
            System.out.println("Your guess was too high");

        //Validates input
        while (!input.hasNextInt()) {
            System.out.println("Invalid response, try again.");
            input.next();
        }

       
    }
}

if the user guessed the number the game ends using break

in while (!guess.hasNextInt()) you were using a integer where it's expect the Scanner input

majkl zumberi
  • 156
  • 1
  • 8
  • Great info! I ended up solving it myself right before checking back to see your code. Essentially we both got the end result with a bit of a difference. I'll drop my code below: – CJarbez Sep 05 '20 at 15:31
1

If you want to parse your number you can use Integer.parseInt() method as shown. Also you are using hasNextInt() incorrectly. You are also not storing in.next() value in any variable.

import java.util.Scanner;

public class GuessNumber {
 public static void main(String[] args) {
    int num = (int)(Math.random() * 101);

    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to my Guessing Game!");

    int guess = -1;
    //Loop goes as long as guess doesn't equal num
    while (true) {
        System.out.print("Guess a number between 1 and 100:   ");
        String numberString = input.nextLine();

        //Validates input
        try {
            guess = Integer.parseInt(numberString);
            if (guess == num) {
                System.out.println("Correct!");
                break;
            } else if (guess < num)
                System.out.println("Your guess was too low");
            else
                System.out.println("Your guess was too high");
        } catch (Exception e) {
            System.out.println("Invalid response, try again.");
        }
       }
    }
}
Saurabh Nigam
  • 795
  • 6
  • 12