-2

I have a string s in which I have to detect the word "car" and print the next 2 words. However, currently when I print words[i1], it gives me "is awesome". but it should give me "is", then "cool", then "is", then "awesome" in different lines(n=2 in the above case).

Can anybody tell me what is wrong with this code?

String s="car is cool car is awesome.";
int index = contents.indexOf(s);
while(index >= 0) {
   String[] words = new String[n];
   for (int i3=0;i3<n;i3++){
       words[i3]="";
   }
   int i1=0, i2=index;
   while(true){
       if(i1==n) break;
       if(i2>=contents.length()) break;
       while(true) {
           if(i2>=contents.length()) break;
           if((contents.charAt(i2)+"")==" ") break;
           words[i1]+=(contents.charAt(i2)+"");
           i2++;
       }
       System.out.println(words[i1]);
       i1++;
   }
    index = contents.indexOf(s, index+1);
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • `if((contents.charAt(i2)+"")==" ")` -> [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Jun 27 '15 at 19:52

3 Answers3

0

You can achieve what you wanted using this piece of code:

String s = "car is cool car is awesome.";
int index = s.indexOf("car ");

if(index >= 0) {
    index += "car ".length()
    String[] words = s.substring(index).split(" ");

    // if you need n words after "car"
    for (int i = 0; i < words.length && i < n; i++) {
        System.out.println(words[i]);
    }
}

If there are not n words after "car" or any given string, this will print the maximum no. of words.

contents is not in the code you shared.

There is a very common bug in your program at this line:

if((contents.charAt(i2)+"")==" "

It should rather be

if( " ".equals((contents.charAt(i2)+"") )
  • There are various separators too apart from " ". eg. ".","," . What can I do to take them into account too? – Sam Sonnell Jun 27 '15 at 20:01
  • You can use a regex expression like this to specify multiple delimiters like this : `.split(" |,|\\.")` – Arun Saragadam Jun 27 '15 at 20:03
  • Outputs `car` then `is` and stops - which is wrong; it should output `is` then `cool` then `is` then `awesome`. – MT0 Jun 27 '15 at 20:18
  • @MT0 Edited my answer. Made a small mistake while writing the code. Forgot to add the length of the string to the index It now outputs min. of `n` or the max. number of words after the given word. Thanks for pointing out. – Arun Saragadam Jun 27 '15 at 20:23
  • 1
    Won't compile you need `"car".length()` - it also doesn't find the second instance of the word `"car"` and will fail on the string `"carrot is cool, car is awesome."` as it finds the `car` in `carrot`. – MT0 Jun 27 '15 at 20:33
  • @MT0 edited it again to consider all the cases... I thought it would serve as a guide to OP. :) – Arun Saragadam Jun 27 '15 at 21:37
  • Need a semi-colon after `"car ".length()` and the OP posted in the comments that there are other separators; so this would still fail on `"I like my car. It is a cool car, it is awesome to drive."` – MT0 Jun 27 '15 at 23:23
0

A solution based on splitting the string:

final int n = 2;
final String sentence="car is cool car is awesome.";
final String[] words = sentence.split("\\W+");
for ( int i = 0; i < words.length; ++i ){
    if ( words[i].equals("car") ){
        for ( int j = 0; j < n && ++i < words.length; ++j )
           System.out.println( words[i] );
    }
}

A different solution based on regular expressions:

final int n = 2;
final String s = "I like my car. My car is cool. It's an awesome car to drive.";
final String word = "car";
final String separatorRE = " ,.";
final Pattern p = Pattern.compile( String.format( "(?<=[%s]|^)%s(?=[%s]|$)", separatorRE, word, separatorRE ) );
final Pattern q = Pattern.compile( String.format( "(?<=[%s])[^%s]+(?=[%s]|$)", separatorRE, separatorRE, separatorRE ) );
final Matcher m = p.matcher(s);
while ( m.find() ){
    int i = 0;
    Matcher mw = q.matcher(s);
    mw.region( m.end(), s.length());
    while ( ++i <= n && mw.find() )
        System.out.println( mw.group() );
}
MT0
  • 143,790
  • 11
  • 59
  • 117
0

I was making a mistake of not updating i2 before break inside the innermost while loop. Now it works correctly. Plus, if we want to add more separators, we can do so by adding more conditions in that same statement using ||.

String s="car is cool car is awesome.";
int index = contents.indexOf(s);
while(index >= 0) {
   String[] words = new String[n];
   for (int i3=0;i3<n;i3++){
       words[i3]="";
   }
   int i1=0, i2=index;
   while(true){
       if(i1==n) break;
       if(i2>=contents.length()) break;
       while(true) {
           if(i2>=contents.length()) break;
           if((contents.charAt(i2)+"").equals(" ")){i2++; break;}
           words[i1]+=(contents.charAt(i2)+"");
           i2++;
       }
       System.out.println(words[i1]);
       i1++;
   }
    index = contents.indexOf(s, index+1);
}
  • Need to fix the first line: `String contents="car is cool car is awesome";String s="car";`. Also, it outputs `car` then `is` then `car` then `is`. – MT0 Jun 27 '15 at 20:41