CJarbez
CJarbez

Reputation: 3

Need to validate int; hasNextInt isn't working

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");

        }
    }
}

Upvotes: 0

Views: 430

Answers (2)

Saurabh Nigam
Saurabh Nigam

Reputation: 823

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.");
        }
       }
    }
}

Upvotes: 1

majkl zumberi
majkl zumberi

Reputation: 180

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

Upvotes: 1

Related Questions