PDA

View Full Version : Java Problem




CayceH
Feb 25, 2012, 09:55 PM
import java.util.Scanner;

public class HelloWorld {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Choose a number between 1 and 10");
if (keyboard == 1) { System.out.println("You are correct");
} else System.out.println("Sorry try again");
}
}


it says i can not have (keyboard == 1) so i tried to declare keyboard as an int but received another error how do i make it to where if someone types 1 on the keyboard that it will run the command?



lee1210
Feb 26, 2012, 03:57 AM
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

Look at hasNextInt() and nextInt(). You need to pull a value from the scanner, then compare that value to 1 or "1" or whatever. Comparing the scanner object itself to 1 doesn't make sense.

-Lee