aspose file tools*
The moose likes Beginning Java and the fly likes Average calculator without sentinel. A little adjustment needed. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Average calculator without sentinel. A little adjustment needed." Watch "Average calculator without sentinel. A little adjustment needed." New topic
Author

Average calculator without sentinel. A little adjustment needed.

Josh Maddy
Greenhorn

Joined: Oct 02, 2012
Posts: 5
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

Joined: Jun 19, 2010
Posts: 825
    
    5

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.


The quieter you are, the more you are able to hear.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

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


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Josh Maddy
Greenhorn

Joined: Oct 02, 2012
Posts: 5
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

Joined: Oct 02, 2012
Posts: 5
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

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Feb 26, 2001
Posts: 4462
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Josh Maddy
Greenhorn

Joined: Oct 02, 2012
Posts: 5
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

Joined: Oct 02, 2012
Posts: 5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Average calculator without sentinel. A little adjustment needed.