views:

135

answers:

7

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 bites you" so the string would be 000100 . Each 0 or 1 represent found and not found respectively. 1 can also be the number of occurances of searched word in the string. For example lets say the word dog appeared 5 times then the coded string would be 000500

its all fine. But when there are more then 9 occurances there is a problem it is like "0001000" which is wrong. Because in my program i have to split all digits ( occurances ) too. So it return me 1 not 10 :( ....

A: 

use hexadecimal.

brian
Then what if you have more than 16 ;-)
Sands
These are the jokes people.
brian
+2  A: 

hehehe sorry, but ... this is funny.

There is no way to know, because you don't have any other identifier to delimit your resulting string. Have you use another separator It might work, but the way your problem is "specified" as of now, there is nothing much you can do about it.

So, if this is homework, I would suggest you to call your teacher and ask him: "What are you to do if there is > 10 occurences"

Unless it is explicitly forbidden in your specifications. I would add an space to the resulting string. So the output would be:

0 0 0 10 0 0 

Instead of

0001000

EDIT

if i put space and try to convert this string into INT to do SUM it give error :(

Well don't include the space then:

    String a = "0 0 9 0";
    String b = "0 0 9 0";
    String [] pa = a.split(" ");
    String [] pb = b.split(" ");

    StringBuilder result = new StringBuilder();

    for( int i = 0 ; i < pa.length; i++ ) {

        result.append( Integer.parseInt( pa[i] ) + Integer.parseInt( pb[i] ) );
        result.append( ' ' );
    }
    System.out.println( r );

Prints:

0 0 18 0

Without ambiguities.

Still I really think that you need a different data structure, if the only reason you're using strings, is because you can store them in files, for later processing, then, you can still change from data strucure. You can use an arrya or something similar in memory and eventually persist it to any representation you like ( I would use object serialization directly )

Once you need to process more information you retrieve your stored data and load it again in memory.

You may consider using a Map where as key you use the word and as value the count:

Something like this

public void add( String s ) {
    if( !map.containsKey( s ) ){
        map.put( s, 0 );
    }
    map.put( s, map.get( s ) + 1 );
}

Each time you find a word, you call this method and that will increase the count in memory.

All your previous questions look really interesting, I think you are stopping just by some technical details. Use a different data structure and continue with your research.

OscarRyz
lol.. its not assignment. whom should i call?? that's why i am here to ask all GURU. I can not us space as i want to calculate two output too ( sum two output) if i put space and try to convert this string into INT to do SUM it give error :(
If you want to use the output for summing, then you are using the wrong data type (String). You should be using int.
Harsha
A: 

If you can have a Tag or an escape sequence for such circumstances, it may work. You would need a broader definition of the problem to use it though.

Sands
I can not use escape seq. or any other string seperator as i want to sum it with other search result too :( so lets say it would be like0010 + 0020 = 0030if i use any string seperator or other stuff it give error for digit formater
agezerboy: I think you reaaaally need a different datastructure. String is not the best tool for this.
OscarRyz
+1  A: 

Why not go with an int array instead of a string to store results in?

Wallacoloo
A: 

You should use a List<Integer> or an array to store your counts, instead of a String of digits. That's the more natural data structure to use here. A String is just a kludge.

Jim Ferrans
what if you have more than 2147483647?
GregS
@GregS: Use List<Long> then
OscarRyz
what if you have more than 9223372036854775807?
GregS
List<BigInteger>?
Grundlefleck
if i get this value 9223372036854775807 i will stop using java and will shift to another lang. LOLso any good solution?
OscarRyz
A: 

You may want to go with some base that is higher than what you may deal with, so, for example, if you go with 0..9..a..z..A..Z then you could have 62 occurances handled.

But, if you want to do a sum you will have a problem, as you will need to loop through and count, rather than just converting it to an int.

If 62 isn't high enough, there are 96 or so printable characters in basic ASCII, be creative. :)

James Black
A: 

The implementation is somewhat strange - or academical ;) The question is answered and accepted but nevertheless, I think such a method should either

  1. mark occurences (x)or
  2. count occurences

The first could be solved with a bitfield like

a dog is a dog is a dog  ->  01001001

The preferred solution for the second task is a lookup table like

"a" -> 3
"dog" -> 3
"is" -> 2

Mixing both could be interesting for implementing a full word index. But then I'd choose:

Map<String, Collection<Integer>>

where the key is a word in the sentence and the value a collection of positions of this word in the sentence.

As I said, not a direct solution to the question but maybe a different soltution to implement the requirements.

Andreas_D