0

Sorry if my question sounds dumb. But some time small things create big problem for you and take your whole time to solve it. But thanks to stackoverflow where i can get GURU advices. :)

So here is my problem. i search for a word in a string and put 0 where that word occur. For example : search word is DOG and i have string "never ever let dog bite you" so the string would be 000100 . Now when I try to convert this string into INT it produce result 100 :( which is bad. I also can not use int array i can only use string as i am concatinating it, also using somewhere else too in program.

Now i am sure you are wondering why i want to convert it into INT. So here my answer. I am using 3 words from each string to make this kind of binary string. So lets say i used three search queries like ( dog, dog, ever ) so all three strings would be 000100 000100 010000 Then I want to SUM them it should produce result like this "010200" while it produce result "10200" which is wrong. :(

Thanks in advance

3
  • 1
    Sounds like everything works up until you get to 10200. Then all you have to do is convert to string, and keep adding '0' to the front until the string is the same length as words in your sentence. Jan 15, 2010 at 18:17
  • It looks as though you put a 1, not a 0 where the word occurs.
    – akf
    Jan 15, 2010 at 18:20
  • what you are doing is NOT an binary addition
    – user177800
    Jan 15, 2010 at 18:31

4 Answers 4

6

Of course the int representation won't retain leading zeros. But you can easily convert back to a String after summing and pad the zeros on the left yourself - just store the maximum length of any string (assuming they can have different lengths). Or if you wanted to get even fancier you could use NumberFormat, but you might find this to be overkill for your needs.

Also, be careful - you will get some unexpected results with this code if any word appears in 10 or more strings.

4
  • Thanks for your reply. I convert from string to int then do sum. is is it possible to retain leading zeros after conferting it into int? if not in INT then any other data type?
    – user238384
    Jan 15, 2010 at 20:33
  • Not any numeric type - the language needs a standard way to represent numeric values to the user, since 1 == 001 == 0001 ... - so even if it wanted to show you leading zeros, it wouldn't know how many to show. That's what NumberFormat is for.
    – danben
    Jan 15, 2010 at 21:00
  • thanks for explaing. Now i am looking for a method that can pad 0 on the left side of the final calculation. if you know can you tell me? I saw some other post about same question on this site but they are not usefull.
    – user238384
    Jan 15, 2010 at 21:16
  • I solved pading problem. Now the same problem occus that you mentioned. about 10 :( how to solve that???
    – user238384
    Jan 15, 2010 at 21:59
3

Looks like you might want to investigate java.util.BitSet.

1
  • How does this help him compute sums?
    – danben
    Jan 15, 2010 at 20:58
1

You could prefix your value with a '1', that would preserve your leading 0's. You can then take that prefix into account you do your sum in the end.

That all is assuming you work through your 10 overflow issue that was mentioned in another comment.

1
  • 1
    +1 This is a really smart and practical way! Sometimes we got caught up with all the classes and the operations that languages offer us, that we forget to think like that! Jan 15, 2010 at 18:35
0

Could you store it as a character array instead? Your using an int, which is fine, but your really not wanting an int - you want each position in the int to represent words in a string, and you turn them on or off (1 or 0). Seems like storing them in a character array would make more sense.

1
  • I think the point is that he wants to be able to sum the positions by summing the ints.
    – danben
    Jan 15, 2010 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.