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);
}