Author | Message |
---|---|
ytsai8
Posts: 47
|
Posted 18:24 Jan 29, 2014 |
What is the correct way to restrict user inputs? I have searched on java.api and I am guessing we should use the hasNext() method? (I am not sure..) anyhow, I have no idea how to set the stuff up. For example, the lab we did today wants users to input military time, and the range should be from 0000 to 2400.. How do we set the method so users can only insert 4 digits that is greater than 0 and less than 2400?
|
Anonymous
Posts: 166
|
Posted 22:00 Jan 29, 2014 |
that would be using if statements and while loops but we are not there yet. but you can ask the user to input only actual military times like this. System.out.print("enter actual military times"). using while loops while run the program over and over again until the user enters valid times |
Eric Liao
Posts: 158
|
Posted 22:24 Jan 29, 2014 |
The input is not restricted by the military format anymore, you are asking use to input the hour first as an integer then the minute. In other word, you would need to do a while loop to get user input until user enter the acceptable number (for example, hour need to be between 0 to 23 and the minute need to be 0 to 59). You can do a do while loop or while loop, whichever one you prefer. For instance, you may do the following: int hour = -1; Scanner in = new Scanner(System.in); while (hour > 24 || hour < 0) { System.out.println("Enter hour( 0 - 23 ): "); hour = in.nextInt(); } |
ytsai8
Posts: 47
|
Posted 09:41 Jan 30, 2014 |
Thanks!! |