• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

an int and a string.

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic