Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Average calculator without sentinel. A little adjustment needed.

 
Josh Maddy
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.



 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • 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.
 
Jelle Klap
Bartender
Posts: 1951
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
Java Python Ruby
  • 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:
 
Junilu Lacar
Bartender
Pie
Posts: 7320
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic