1

Could someone tell me how to put an error message in to the following code? If the user enters a number that is not between 0 and 12, how do I output "Invalid entry".

The program at the moment works fine and if an invalid character is entered the user is allowed to try again.

int hours; do { System.out.print("Enter hours: "); hours = myscanner.nextInt(); } while (hours < 0 || hours > 12); 
7
  • 5
    if(....) System.out.println("Error"); ? Commented Dec 9, 2013 at 15:49
  • If the user enters a number that is not between 0 and 12, then the loop continues cycling. Are you sure this is what you want? Commented Dec 9, 2013 at 15:50
  • shouldn't this condition be inverted: hours < 0 || hours > 12 ? Commented Dec 9, 2013 at 15:50
  • His condition is fine: anything below 0 or above 12 is invalid. Commented Dec 9, 2013 at 15:54
  • He said in his question that the condition is fine. He just wants an extra message when they enter something invalid Commented Dec 9, 2013 at 15:56

3 Answers 3

2

I would use an "infinite" while loop and break out of it when the hours figure is valid. while(true) { ... } is idiomatic Java.

Scanner scanner = new Scanner(System.in); int hours; while (true) { System.out.print("Enter hours: "); hours = scanner.nextInt(); if (hours >= 0 && hours <= 12) { break; } System.err.println("Invalid entry (should be 0-12)"); } 
Sign up to request clarification or add additional context in comments.

1 Comment

It would also be nice to catch the very likely exception "InputTypeMismatch", but it's not part of the question...
0
int hours; boolean valid; do { System.out.print("Enter hours: "); hours = myscanner.nextInt(); valid = (hours >= 0 && hours <= 12); if (!valid) System.out.println("Invalid entry"); } while (!valid); 

NOTE: I've added a variable, because otherwise, you'd have duplicate condition there NOTE2: I've also reverted the condition because I don't like if booleans are referring to negative condition(I prefer valid over invalid)

4 Comments

Cruncher: I made a few typos when I was writing it so there was a time, when there was complete gibberish in the answer
Thanks for your help. The error message now works but it doesn't loop back to allow for re-entry of a digit.
@HungryHenno It should
I don't know why but it won't work but I'm going to use the boolean method mentioned above. It works perfectly. Thanks.
-1
int hours; do { System.out.print("Enter hours: "); hours = myscanner.nextInt(); if (hours < 0 || hours > 12) System.out.println("Please insert a valid entry"); } while (hours < 0 || hours > 12); 

4 Comments

If you have the if statement, no reason to check the condition twice. Change it to a while(true) and break when you get a good input maybe? p.s I don't think this is worth a downvote. Can the voter explain?
@Cruncher of course, there's already the aetheria proposition for that. My (obvious) solution is just for not upset the initial code.
@mauretto My comment was 7 minutes before aetheria's answer :)
@Cruncher LOOOL btw my intent was really to not upset his reasoning! ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.