• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Average calculator without sentinel. A little adjustment needed.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone. This simple java program that calculates average scores without using sentinel.

1) The user is asked how many courses will be entered at the start.
2) There is a condition that the number of courses can't be more than 4. If more than 4 courses are entered, the program terminates. The "if condition" at Line 17 checks that.
3) Is there any way that if more than 4 courses are entered entered, the program goes back to the line 14, instead of terminating?

The program runs fine but I want it to go back to Line 14 and again ask user to enter the number of courses, whenver a user enters and "invalid" value. Please help.



 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about putting it in a while loop? Just create a condition that will get your user out of it once he decides to terminate the program.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to JavaRanch!
Let me ask you, if you forget about this console I/O business for a moment, how would you normally execute the same body of code an arbitrary number of times?

Edit: Ugh, too slow..
 
Josh Maddy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kemal Sokolovic wrote:How about putting it in a while loop? Just create a condition that will get your user out of it once he decides to terminate the program.



It's the same in while loop. When the condition comes true, the program terminates naturally. However, I want it to prompt me to enter the information again, in case of an "invalid" value.
 
Josh Maddy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jelle Klap wrote:Hi, and welcome to JavaRanch!
Let me ask you, if you forget about this console I/O business for a moment, how would you normally execute the same body of code an arbitrary number of times?

Edit: Ugh, too slow..



Thanks, Jelle. I guess I would use if-else or while loop. But then, my problem is about I/O business entirely. If I enter an "invalid" value, the program terminates normally. But instead I want it to ask me to enter the value again.

PS. Why this "Ugh, too slow" jibe? Lolz. Thanks anyway.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Josh Maddy wrote:

Kemal Sokolovic wrote:How about putting it in a while loop? Just create a condition that will get your user out of it once he decides to terminate the program.



It's the same in while loop. When the condition comes true, the program terminates naturally. However, I want it to prompt me to enter the information again, in case of an "invalid" value.



If I understood the problem well, you want to prompt for input over and over again, until the user enters a valid value (0, 1, 2, 3 or 4). You just need to put the code where you read user's input into while loop, that will repeat until the value entered is valid. One solution is to put it inside infinite loop that breaks once the input is valid:
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sound more like a job for a do-while loop rather than an infinite loop. Rather than put everything in one method, why not try to break the story down into smaller pieces: getValidInput, getNextNumber, getAverageSoFar, etc.

 
Josh Maddy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kemal Sokolovic wrote:
If I understood the problem well, you want to prompt for input over and over again, until the user enters a valid value (0, 1, 2, 3 or 4). You just need to put the code where you read user's input into while loop, that will repeat until the value entered is valid. One solution is to put it inside infinite loop that breaks once the input is valid:



Yes, that's right. Thanks Kemal.
 
Josh Maddy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Sound more like a job for a do-while loop rather than an infinite loop. Rather than put everything in one method, why not try to break the story down into smaller pieces: getValidInput, getNextNumber, getAverageSoFar, etc.



Thanks for the tip. I will try that.
 
Let's get him boys! We'll make him read this tiny ad!
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!