• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Java problem  RSS feed

 
Ming Daines
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm trying to write a simple program using the "Repeat until sentinel" method but can't seem to get the "smallest" number to print correctly. Everytime I enter the SENTINEL value of 0 to stop the program, it prints out 0 as the smallest number... even though it's not supposed to go into the comparison IF...ELSE loop. Forgive me for the simple question but what am I doing wrong?

Thanks much!
Ming

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are always given default initial values if you don't initialise them explicitly. In the case of ints, the default value is 0. So small is initialised to 0 and will remain 0 unless you enter a negative value.
Try changing line 14 to
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you enter 3, then 4, there are chances that you'll never initialize the small variable. It's default value is 0, that's why it may print 0. You have to initialize small to the first entered value.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you taking in consideration that the default value of an int 0 is? So you must type a negative number first or a high number and then a lower number to set the value small.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys are way too slow
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program works as is only if you enter numbers in a descending order because you did not take into account default values of 0. If you enter in your numbers getting increasingly larger you will always return small's default value of 0.

Edit: Sheesh, I click reply...phone rings, I talk for a few minutes...come back and respond...and bam multiple responses beat me to the punch!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:You guys are way too slow

Your snippet does not solve the problem. He'll end up with a small value of Integer.MAX_VALUE
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
Joanne Neal wrote:You guys are way too slow

Your snippet does not solve the problem. He'll end up with a small value of Integer.MAX_VALUE


You're right. It's oldVal that needs to be initialised or initialise small to the first entered value as you suggested.
 
Ming Daines
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your replies! (''); I added Integer.MAX_VALUE to oldVal and it works like a charm. Does this act like an empty container that doesn't have an initial value and takes any integer up to its max? I tried looking up this term and couldn't quite figure out what it does.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a public static final int value from the Integer class which holds the highest value that an int can hold.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, MAX_VALUE isn't empty at all. It is a "constant" representing the highest value which can be expressed in 32 bit two's complement, ie 2 to the 31st - 1, or 0x7fffffff or 2147483647 (I think). All those numbers should give the same value.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


nobody watched this.this should be dependent on some condition.whethere user wil enter any no or no.or whether user wants to continue or not
 
Ming Daines
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for helping me learn something new every day
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:

nobody watched this.this should be dependent on some condition.whethere user wil enter any no or no.or whether user wants to continue or not

Using while(true) and break isn't necessarily good or bad. Opinions on that matter differ. If you wish to discuss that then please open a new thread.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:You guys are way too slow

*lol* That was awesome--three answers separated by ~2 seconds
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:nobody watched this.this should be dependent on some condition.whethere user wil enter any no or no.or whether user wants to continue or not

Loop-forever constructs are quite common.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!