• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with variables incriments in an else if statment  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. I have very little programming experience and could use a little hint or suggestion. My programming is probably pretty ugly but please bear with me. My while loop seems to work fine and stops at 8 slips but for some reason when the averages on lines 46 and 47 are being calculated they seem to be using the initialized values of 0 which are on lines 17 and 18. for some reason the headsCount and tailsCount variable on lines 32 and 36 respectively aren't working the way I want them to. Also at the end of my comments section it says "It should also provide stats on the percentages of each outcome of a flip." but really there doesn't need to be any kind of randomizing feature, it's merely the average of the input of "h" and "t". Any help, feedback, and hints would be much appreciated!

 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hint: when comparing strings, in most cases, you want to use the equals() method, as you want to compare the values for equality, and not if the two string objects are the same object.
 
Tom Sinclair
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice Henry!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to use equals/equalsIgnorecase in while loop, if, else condition checking. Equals checks the both the object references/address are same or not,on the otherhand == checks values in both the object are same / not.
 
Tom Sinclair
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks satya chowdary!
 
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satya chowdary wrote:You have to use equals/equalsIgnorecase in while loop, if, else condition checking. Equals checks the both the object references/address are same or not,on the otherhand == checks values in both the object are same / not.


equals() in the String class only checks for the same sequence of characters, not if the two reference variables refer to the same object. It turns out that if Strings are inturned that, then those two things could be inherently true. Now,the equals() method initially defined in the Object class does check specifically for object reference.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Rowe wrote:
equals() in the String class only checks for the same sequence of characters, not if the two reference variables refer to the same object. It turns out that if Strings are inturned that, then those two things could be inherently true. Now,the equals() method initially defined in the Object class does check specifically for object reference.


Well, let us check the source from Java jdk1.7.0_13
It turns out that the very first thing String 's equal checks happens to be...

 
Josh Rowe
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:Well, let us check the source from Java jdk1.7.0_13 ...

It looks like you are right, but the only reason it does that is to return true automatically in constant time, rather than having to search in linear time even if they are the same object. It is not a matter of whether the reference variables are the same object, but if they contain the same the same contents.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Rowe wrote:It looks like you are right...

Josh,

I hope you don't mind, but I removed Ivan's entire post from your reply. When "quoting", you only need to include as much as needed to make it clear what you're replying to (and that generally shouldn't include code). Please use the 'Preview' button before you post, so you know that what you're sending is what you intended.

Thanks.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!