File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes cant add twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cant add twice" Watch "cant add twice" New topic

cant add twice

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm inside this method and want to add 2 to a var. Next, also inside the same method, i want to add to the same var lets say 2(again). I expected to get var = 5, but no!
I allways get ...var = 2
Looks like everytime i add, the adding starts from beguinning, rewriting my var, not adding to the previous value.
Well, i tried static and non static vars, sets and gets, i dont know what else to do
Can someone pls help me?

java amateur
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Please post your code and we'll have a look

Mike Gershman
SCJP 1.4, SCWCD in process
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks for answering
ok here goes a bit of it:

I just cant spot where i'm doing it wrong.
Imagine 1rst and 3rd conditions meet - then hmm = 4, but no! Allways hmm =2!
Dont get it.
[ April 16, 2004: Message edited by: miguel lisboa ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The "+=" operator adds the left operand to the right operand. But note the order of the two symbols: "+=". You've written
hmm =+ 2;
writing "=+" instead of "+=". The compiler is interpreting this as
hmm = (+2);
thus your variable is always set to 2.

[Jess in Action][AskingGoodQuestions]
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks for your help, in first place
i'm using two separated checking methods, and both dont add twice, even with +=
take a look:
i use this method:

and also the hmm variable.
have a look, pls:

when two conditions meet, var hmm should be 4 or 5 and then the string should print either "Dois Pares" or "Full House!", but never never does...
i really puzzled
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
Shouldn't you be passing hmm to the contador() method instead of hardcoding 2 or 3 as you have here "contador(2)"? If I read this right if you have a pair, hmm = 2, if you have another pair hmm = 2 + 2, when you call contador() you'll be passing hmm which should equal 4 which gives you "Dois Pares" which I'm guessing means "Two Pairs".
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
what i'm trying to do is to spot a "Two Pairs" or "Full House"" hand.So i'm adding 2 or 3 to that var each time i find two or three of the same kind, and is the var adds 5 i print Full House andif it values 4 i can for sure print Two Pairs. The problem it that my "adding method" doesnt work
I've two parallel ways of doing it: with an outside method contador() AND with internal var "hmm". but BOTH DONT add twice. Dont know what else to do
btw, thanks for answering!
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
what was i doing wrong?
Using a return after every if, so the program you stop executing the method and hence wouldnt see the other number to add
Many thanks to all who helped!
I agree. Here's the link:
subject: cant add twice
It's not a secret anymore!