1

I have a Java assignment and cannot get it to work.

I am doing a guessing game between 1-100. When I run my code it keeps telling me "too low" whether it is correct or too high.

Here is my code:

public static void main(String[] args) throws java.io.IOException {

    int i, ignore, answer = 64;

    do {
        System.out.println("I'm thinking of a number between 1 and 100.");
        System.out.println("Can you guess it?");

        i = (int) System.in.read();

        do {
            ignore = (int) System.in.read();
        } while (ignore != '\n');

        if (i == answer) System.out.println("**RIGHT**");
        else {
            System.out.print("...Sorry, you're ");

            if (i < answer) 
                System.out.println("too low");
            else 
                System.out.println("too high");
            System.out.println("Try again!\n");
        }
    } while(answer != i);
}
Majora320
  • 1,321
  • 1
  • 13
  • 33
  • 2
    Because with the call to `System.in.read()`, you are getting one `char`. You should construct a `Scanner` and call it's `nextInt()` method, like so: `Scanner scan = new Scanner(System.in); i = scan.nextInt();` – Majora320 Mar 22 '16 at 00:26
  • 1
    `System.in.read()` takes the next **byte** not the next **int**. There is a big difference – OneCricketeer Mar 22 '16 at 00:29
  • 1
    You should use debugger or even just print the variable out to see what your code is actually doing before you ask without any test. If you did, write them into the question. – Nier Mar 22 '16 at 00:35
  • Thank you everyone. Nier, I have not learned how to debug in Java yet. This is my first class. I did not think about the System.in.read() only returning char. I will try another way. Thanks a lot Majora320! – Magdalina08 Mar 22 '16 at 01:09

1 Answers1

1

Because System.in.read() returns a char object representing the character which was typed. Casting it to an int will return, instead of the actual integer that was typed, a char object that has a completely different value.

To solve this problem, you should use the Scanner class, which has a nextInt() method that is perfect for this. It will throw an InputMismatchException on invalid input, so if you want error handling, you should catch that.

Here is a working (and slightly cleaned up) version of your code:

import java.util.Scanner;
import java.util.InputMismatchException;

public class Guess {
    public static void main(String[] args) { // No need to throw IOException
        int input = -1, answer = 64; // Initialize input for if the user types
                                     // in invalid input on the first loop

        Scanner scan = new Scanner(System.in);

        do {
            System.out.println("I'm thinking of a number between 1 and 100.");
            System.out.println("Can you guess it?");

            try {
                input = scan.nextInt();
            } catch (InputMismatchException ex) {
                System.out.println("Invalid Input!");
                continue; // Skips to the next loop iteration if invalid input
            }

            if (input == answer) 
                System.out.println("**RIGHT**");
            else {
                System.out.println("...Sorry, you're too " + (input < answer ? "low" : "high"));
                // ^ Ternary operator; you may not have learned this yet, but it 
                // just does a conditional return (if the value before the '?' is
                // true, then return the value before the ':'; else return the 
                // value after.)
                System.out.println("Try again!");
            }
        } while (answer != input);
    }
}
Majora320
  • 1,321
  • 1
  • 13
  • 33
  • This worked great. Thank you so much for your help. I didn't know System.in.read() would not return int. The example I was going from was using letters instead of numbers. I was trying to modify that code. Makes sense now! – Magdalina08 Mar 22 '16 at 01:43