File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While Loop Not Reading First Integer

 
Collin Sampson
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9497
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaps, please UseCodeTags.

Thanks.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is an idiom you would never guess, ensuring you read the number once per loop.
 
Winston Gutkowski
Bartender
Pie
Posts: 9497
50
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9497
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed the count++. Sorry. You would have to put the count++ in {} after the first else. Then it would ignore marks of 101%.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic