• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Why 'do-while' is ruuning more than once?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. This is a program that prompts a help menu using the do-while iteration. If user input is not valid, the menu should be reprompted once. But when I run the code and enter an invalid value, the menu is prompted 3 times. Please tell me why is this happening?

 
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't it pretty obvious? When the user enters a number, the do body is executed first, the while condition is checked and since the user enters 6, the while satisfies, the do body is executed once again!
 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joe Harry wrote:Isn't it pretty obvious? When the user enters a number, the do body is executed first, the while condition is checked and since the user enters 6, the while satisfies, the do body is executed once again!


Actually no. when i run the code, first the 'do' body is executed, then it goes to the while loop to check if my choice is a valid char like anything 1 to 5. if i enter anything else, the do body should rerun once so that i may enter a valid value. But surprisingly, if i enter for example 6 or 0, i see that the 'do' body is executing 3 times. That means the 'do' body is shown 3 times repeatedly on the screen, but it should have been shown only once.
 
Joe Harry
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The do-while loop is an exit-condition loop. This means that the body of the loop is always executed first. Then, the test condition is evaluated. If the test condition is TRUE, the program executes the body of the loop again. If the test condition is FALSE, the loop terminates and program execution continues with the statement following the while.



I tried your program and here is what I see!

Joe:Desktop joe$ java Menu
help on:
1. If
2. Switch
Choose a option:
7
help on:
1. If
2. Switch
Choose a option:
help on:
1. If
2. Switch
Choose a option:



the body is first executed, the test condition fails, the body is executed again! So effectively it is 2 times the help on gets printed when I enter anything less than 0 or greater than 5!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This happens because of how System.in.read() works.

It reads a byte from the console. When you for example type "6<enter>", then on Windows there will be 3 bytes in the input: the character 6 followed by a CR and an LF (carriage return, line feed) character.

What happens is this: first the 6 is seen, which is bad input, so the loop runs again, displaying the menu and reading the next byte. Then the CR is seen, which is bad input, so the loop runs again, displaying the menu and reading the next byte. Finally the LF is seen, and the same thing happens again.

There are better ways to read input from the console, for example using java.util.Scanner.nextLine().
 
Marshal
Posts: 64696
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote: . . . There are better ways to read input from the console . . .

I find it hard to think of a worse way to read from the keyboard than System.in.read().
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!