search the arraylist
given the below code:
import java.util.ArrayList;
publicclass Test{
/**
* @param args
*/
publicstaticvoid main(String[] args){
ArrayList myList =new ArrayList();
myList.add("I have a book of Peter and Jane");
myList.add("My car is parked, in its place where Peter left and somehow its gone");
myList.add("The treusure that was once lost, and now is found");
myList.add("Has anybody found my shoes?");
myList.add("This drink smells awesome!");
myList.add("A dead body's missing for a long while now and we should be prepared");
myList.add("Late is the hour in which I am writing this example and I feel extremely tired");
myList.add("Knights, your swords. Knight for honour; now CHARGE!");
}
}
how can i make some sort of a search engine, so that it will be able to find a particular word, and return the sentence in which the word is in..Ex:
Java TestKnight
>Knights, your swords.Knight for honour; now CHARGE!
Java TestPeter
>I have a book ofPeter and Jane
>My car is parked, in its place wherePeter left and somehow its gone
can you help pls?
Two ways are possible, depending on how much work you want to put in this.
The easy way is to go through the whole List and use the String#contains method.
The harder and faster ways would be to index the content.
If you want to search through a lot of text a lib like Lucene would be the best way to do that.
easy way:for(String s:myList){if(s.contains(word)) System.out.println(s);}
for (int i = 0; i < myList.size(); i++) {
String str = (String)myList.get(i);
if (str.indexOf(args[0]) > -1) System.out.println(str);
}
sorry neither of them worked: where there latter posted code, it responded by:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0at Test.main(Test.java:39)
Actually both versions will work but you seem to not know how to use them.
> sorry neither of them worked: where there latter
> posted code, it responded by:
>
> Exception in thread "main"
> java.lang.ArrayIndexOutOfBoundsException: 0
> at Test.main(Test.java:39)
This sounds like you havn't supplied a command line parameter...
i am not understanding the code: i believe that OutOfBounds refers to an error which somehow Java is searching outside the loop limit where there is nothing..
the former code, Java tells me that cannot convert lists to strings...:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from element type Object to String
Looks like we have to gaze into our crystal balls to see the updated code.
Post your modified code here.
> the former code, Java tells me that cannot convert
> lists to strings...:
>
> Exception in thread "main" java.lang.Error:
> Unresolved compilation problem:
> Type mismatch: cannot convert from element type
> e Object to String
>
That's probably because you're not using generics in your list.
Either use generics or add a cast to String to the loop.
Generics would be the better option...
Try this:ArrayList<String> myList = new ArrayList<String>();
> for (int i = 0; i < myList.size(); i++) {
> String str = (String)myList.get(i);
>if (str.indexOf(args[0]) > -1)
> System.out.println(str);
> }
Still haven't upgraded to 5.0? ;-)
1.4.2_10 all the way baby! :-D
i got the 1.6 ..
the full code:
import java.util.*;
/**
*
*/
/**
* @author <a href=mailto:stefattard@gmail.com> Stefan Attard</a>
*
* @version 1.0
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList myList = new ArrayList();
myList.add("I have a book of Peter and Jane");
myList.add("My car is parked, in its place where Peter left and somehow its gone");
myList.add("The treusure that was once lost, and now is found");
myList.add("Has anybody found my shoes?");
myList.add("This drink smells awesome!");
myList.add("A dead body's missing for a long while now and we should be prepared");
myList.add("Late is the hour in which I am writing this example and I feel extremely tired");
myList.add("Knights, your swords. Knight for honour; now CHARGE!");
Collections.sort(myList);
int index = Collections.binarySearch(myList, "This drink smells awesome!");
System.out.println("Found Knight @ " + index);
String word = "Knight";
for(String s:myList)
{
if(s.contains(word))
System.out.println(s);
}
}
}
import java.util.*;
/**
*
*/
/**
* @author <a href=mailto:stefattard@gmail.com> Stefan Attard</a>
*
* @version 1.0
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<String> myList = new ArrayList<String>();
myList.add("I have a book of Peter and Jane");
myList.add("My car is parked, in its place where Peter left and somehow its gone");
myList.add("The treusure that was once lost, and now is found");
myList.add("Has anybody found my shoes?");
myList.add("This drink smells awesome!");
myList.add("A dead body's missing for a long while now and we should be prepared");
myList.add("Late is the hour in which I am writing this example and I feel extremely tired");
myList.add("Knights, your swords. Knight for honour; now CHARGE!");
Collections.sort(myList);
int index = Collections.binarySearch(myList, "This drink smells awesome!");
System.out.println("Found Knight @ " + index);
String word = "Knight";
for(String s:myList)
{
if(s.contains(word))
System.out.println(s);
}
}
}
