aspose file tools*
The moose likes Java in General and the fly likes an int and a string. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "an int and a string." Watch "an int and a string." New topic
Author

an int and a string.

Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hello all, I had one query. When you append an int to a string, doesn't the variable get assigned a string. For instance I am having a hard time with the following code.



I am using bit shifting, my output looks great for this program; I have to implement, however, when I try to count how many 0s and 1s it get way off a way off answer. Is there something I am missing?
The out put is the following.



I am basically looking on get the following numbers 26 5 4 which correspond to the following: total # of 1 bits, longest 1 bit sequence, longest 0 bit sequence. Thus, the answer should be 26 5 4. I have tried many different ways to implement this and I keep on gettting the same end result. Am I missing the big picture? Please correct me if I am wrong. You enter a key on the keyboard, which corresponds to a ascii code, which then corresponds to a bit pattern, which the computer really works with. I know java uses unicode(could this have something to do with why I am getting this kind of result). Any suggestions much appreciated. Thanks
Jan-Jaap van Nieuwkerk
Greenhorn

Joined: Jul 03, 2006
Posts: 11
Hi,

If i am understanding you correctly this might be the case.

If i am looking at your loop where you count i get the impression you are doing this:
When i add 1 + 2 + 3 + 4 + ... + 26 i get 351 does that suggest where you might have got it wrong?



good luck
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hi, I don't understand what you are saying. I am incrementing by 1 if a 1 is found in the string and incrementing by 1 if a zero character is fount. I am also keeping track of the total 1s in the string. I think my logic is correct here. If not please point out where I am missing out. Thanks
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Actually, its late here. I see what you know mean. I just have to think of a way to count consective 1s and 0s. Cheers.
Thanks again
Jan-Jaap van Nieuwkerk
Greenhorn

Joined: Jul 03, 2006
Posts: 11
Edit: you posted your answer while i was writing this.
good luck

Hi i'll try to say it in another way.

You try to add 1 to countTotalOne by doing the following: countTotalOne+=countOne
if you change that to adding 1 to CountTotalOne instead of the valy of o you get 26 instead of 351 as output.

As i said, look at the counters in your loop and what value you are actualy storing in your counters.

With a change to the abovementioned line i get this output

de ad be ef 42
1101111010101101101111101110111101000010
Total: 40
26 26 14

Now the first number is correct.
The other 2 numbers (26 14) suggest that you do not reset your counters for longest #1 sequence and longest #0 sequence.
You are at the moment counting all ones and zeroes that are in your string.
try to reset your 1 and 0 counter the moment you find a longer string then the one you have found before.

Good Luck.
[ September 29, 2006: Message edited by: Jan-Jaap van Nieuwkerk ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I saw the topic "an int and a string" and was really hoping for "walked into a bar..." Guess I'll wander down to MD.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: an int and a string.