Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

While Loop Not Reading First Integer  RSS feed

 
Greenhorn
Posts: 17
  • 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!

 
Greenhorn
Posts: 4
  • Likes 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:

 
Greenhorn
Posts: 12
  • Likes 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.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaps, please UseCodeTags.

Thanks.

Winston
 
Marshal
Posts: 59742
188
  • Likes 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
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 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
Marshal
Posts: 59742
188
  • Likes 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
  • 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
Marshal
Posts: 59742
188
  • 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
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • 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
Marshal
Posts: 59742
188
  • 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%.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!