• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bad sentinel loop structure for intro to java class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have constructed a basic program that adds up the number of entries for numbers in groups of ten (for instance, if you enter 33 it goes in the 31 - 40 section.) It is stored in arrays and i am trying to make a while sentinel loop that will keep the program going. However, it fails to keep running or never ends. Can anybody help?

Code:

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Aaron Zamecnik,

Welcome to CodeRanch!

Its good to see that you've used code tags in your very first post. And you've also posted a complete code which does not contain any compilation error

However, please TellTheDetails instead of simply mentioning that 'it doesn't work'.

Now coming to your problem, I could see some flaws in code:
1) You are asking user to enter 1 if it wants to continue (user will say 'no' to 'are you done' if it wants to continue). But your while condition is : 'done != 1'. This means, if user wants to continue, it is actually giving wrong input to code (or rather, code is expecting wrong input). The question should be - 'do you want to continue? 0 for yes and 1 for no' (to me, 0 for yes and 1 for no looks a little weird, but that can be fixed laterone )
2) After the while loop, you've put an interesting condition in for loop. You are initializing i with 1, and then comparing it with 11 (i == 11) which will never be true, and program will never enter in for loop.

Apart from that, I think these 9-10 if conditions are redundant. This work can be simply done by taking modulus (% operator).

I hope this helps.
 
Aaron Zamecnik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! It now works well, i removed the 1 for yes and 0 for no. I decided a string was a much better way.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should consider some arithmetic on the “county” number. You realise that the index you are incrementing is the same as county / 10, so in this special instance you can dispense with all the if-elses?
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I meant to say /10 itself for county. By mistake, I mentioned % operator
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aaron Zamecnik wrote:Thank you! It now works well, i removed the 1 for yes and 0 for no. I decided a string was a much better way.


You are welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic