File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes While Loop Not Reading First Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While Loop Not Reading First Integer" Watch "While Loop Not Reading First Integer" New topic
Author

While Loop Not Reading First Integer

Collin Sampson
Greenhorn

Joined: Feb 27, 2012
Posts: 17
This is my code, I am new to java so bear with me. I have to make a program that reads in exam scores and outputs, number of A's, B's...etc. and percentages relatively. I am using a while loop but it is not reading the first score. I input 90 90 90 90 90 60 60 60 60 60 -1(sentinel value) and it outputs that there are 4 A's, and 5 D's. Help!

gimmy kaye
Greenhorn

Joined: Feb 27, 2012
Posts: 2
I miss what this question has to do with servlets.

I think there's something wrong with the logic of yor program.

try this loop:

Tim Hagberg
Greenhorn

Joined: Jan 29, 2010
Posts: 12
The first time you call nextInt() (right before your while loop), you're reading your first number in. Then before you do anything with that value, within the body of the while loop you are calling nextInt() again.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Chaps, please UseCodeTags.

Thanks.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
That is an idiom you would never guess, ensuring you read the number once per loop.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Collin Sampson wrote:I am using a while loop but it is not reading the first score. I input 90 90 90 90 90 60 60 60 60 60 -1(sentinel value) and it outputs that there are 4 A's, and 5 D's. Help!

I think gimmy covered it, but there is a way of doing that doesn't involve repetition, viz:and, as you can see, you don't have to check for a range every time.

I did wonder what happened to the 'E's though.

Winston

[Edit] Too slow
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
I added code tags to one post and you can see how much better it looks, but the other wasn’t indented.
Collin Sampson
Greenhorn

Joined: Feb 27, 2012
Posts: 17
Thank you all for your help, I really appreciate it. I see where I went wrong. Sorry im such a noob =D. This is my first loop program and we haven't really covered it yet in class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
This is slightly changed from what Winston Gutkowski wrote: . . .

Winston
I have changed that slightly, replacing the continue with an else, so as to maintain structured programming. There, everybody can disagree with me about structured programming

Winston Gutkowski wrote:[Edit] Too slow
I’ve been taking lessons from Rob!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Campbell Ritchie wrote:I have changed that slightly, replacing the continue with an else, so as to maintain structured programming. There, everybody can disagree with me about structured programming

Hey, I grew up with Dijkstra

Removing the continue means that count will include invalid entries, which I assumed was not Collin's intention.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
I missed the count++. Sorry. You would have to put the count++ in {} after the first else. Then it would ignore marks of 101%.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: While Loop Not Reading First Integer
 
Similar Threads
Test score
example of count++
this problem baffles me
What is he saying?
need help please?