Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

printing a value after a sentinel  RSS feed

 
Bob Joness
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm currently working through a book called Java By Dissection (as recommended by my Uni) and i've got really stuck on one of the exercises.
It asks to write a program that reads in integers until a sentinel value of 0 is entered. This is no problem. However it then says that after the sentinel value is entered that the program should print out the smallest number, other than the 0, that was entered by the user.
At the moment the only way i can see of doing this is by creating an array to store the integers that are input by the user in order to search for the smallest value. However as arrays have not yet been introduced in the book i guess they don't expect u to use that method. Each time a new integer is entered the old one gets overwritten due to the fact i only have 1 variable to store it. So how could i get the program to search for the smallest value? There is no limit on the amount of integers to be input otherwise i would create a variable for each integer. Does anyone have any ideas on this?

thanks
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about this:

What would happen if you had a variable named smallestIntSoFar? What would that do for your program?

Let us know if this was the hint that you needed, or if you are stuck on something else.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't look at this until after pondering Joel's hint.

Second hint, if needed:

Consider this input sequence:What values would go in the smallestIntSoFar column? What steps did you take to figure that out?

Success?
 
Bob Joness
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, i figured it out at last and it works. Today i saw the problem i was having in a whole new light and sorted it out in no time.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to hear it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!