
Originally Posted by
jmd
yes i used the math.floor as jake recommended and it worked great
i have a question about checking the inputted from user
i want it to check if the user input a integer set in a range, and not allow them to input characters, symbols or words,
how could i go about doing this?
@JMD
Hey, I know what you mean; I use this all the time to demand the user enter an integer:
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
while (!input.hasNextInt()) // while the next token in the buffer is not an integer
{
input.next(); // advance to next token
System.out.println("That is not an integer; try again."); // deliver error message
}
int x = input.nextInt();
// after loop exits, assigns the int to var x (or replace this with whatever you want to do with the int)
A vague concept of what's happening is:
program enters the while loop, checks the scanner buffer to see if the next token is an int; if not, input.next(); moves to the next token (separated by the delimiter, whitespace), and checks for an int. The program will prompt the user to input something within that loop as well. After an int is found, the program exits the while loop, and the assigns the int to variable x, as it is the nextInt(); in the buffer, i.e., the next token in the scanner buffer is an int.
That's the best way I can explain it with my knowledge, and may not be entirely accurate description. But, it works.
Norm is right to say that you can't prevent the user from entering other datatypes; you weren't being very precise with your word choice, however, I know what you meant.