I am trying to store a file into an ArrayList and then have user input from console search for an element in the Arraylist and return found or not. It keeps giving me -1.

public class arrayl {
    public static void main(String args[] ) throws FileNotFoundException
    {
        System.out.println("Please enter title of book: ");
        Scanner input = new Scanner(System.in);
        String search = input.next();
        //file with absolute path
        Scanner file = new Scanner(new File("c:\\Documents and Settings\\name\\Desktop\\Book.txt"));        
        //arraylist named as test
        ArrayList<String> test = new ArrayList<>();
         //creating loop that will read file   
         while (file.hasNext()){
             //adding file to arraylist
            test.add(file.next());
        }
//Using users input, named search, to find  element in the arraylist and print return found or not 
          System.out.println(test.indexOf(search));
file.close();
    }

Recommended Answers

All 16 Replies

add a print in your while method that prints every value added, then add a print for your search value, and see if that value is indeed in the list.

Did you check what is added to your test variable? That would help you understand what is added and why it is not found.

for (String item : test) { System.out.println(item); }

PS: stultuske is faster :(

@stultuske i added System.out.println(test);
System.out.println(test.indexOf(search));

        and it printed the entire file but also printed -1 for each line.  

Here is the output of it..

Please enter title of book
Java
[B:1:Java:Technical:2.00:3]
-1
[B:1:Java:Technical:2.00:3, B:1:Java2:Technical:2.05:3]
-1
[B:1:Java:Technical:2.00:3, B:1:Java2:Technical:2.05:3, B:1:Java2:Technical:2.10:3]
-1

Is it only searching the entire line and not each character?

@Taywin I also added that line and the ouput is below:

Please enter title of book
Java
B:1:Java:Technical:2.00:3
B:1:Java:Technical:2.00:3
B:1:Java2:Technical:2.05:3
B:1:Java:Technical:2.00:3
B:1:Java2:Technical:2.05:3
B:1:Java2:Technical:2.10:3

When you use indexOf(), the item value will be compared using equals() method. That may be what you were not expecting.

The only thing that is in the book file is the following:

B:1:Java:Technical:2.00:3
B:1:Java2:Technical:2.05:3
B:1:Java2:Technical:2.10:3

indexOf for ArrayLists looks for the search object in the ArrayList. The search object is the String "Java". There is no object in the ArrayList that is equal to the String "Java".
You will have to loop thru the ArrayList testing each String in turn to see if it contains the search String. For that you could use String's indexOf method which is NOT the same as ArrayList's indexOf method.

When I try equal() method, it just returns false. Is it only searching by line and not each character in each line?

How does the String's indexOf method work?

check the api's. basically, String.indexOf(substring) returns either -1 if that substring is not in the original String, or the starting index of the substring.
String api

OK, I am trying this now...

The api example looks like it is storing the array into a string object. I just made my file an ArrayList. I am trying to figure out how to convert String search into an object. Any suggestions?

i think you have to use contains() method of arraylist.Also you can pass matching string as an argument in contains method.

I hope this might be working.

no. iterate over each element, they are all String objects. next, use the indexOf method. I think you are just comparing the title, while the elements of your list contain more information, so contains won't find it, same reason as to why equals doesn't find it.

I just want to throw in some thought. I believe using indexOf() will be case-sensitive. If you want a quick case-insensitive method (without calling toLowerCase() or toUpperCase()), use matches("(?i).*"+search+".*"), which returns true if the string contains the search stubstring and match it with case-insensitive, inside the iteration.

or:

String s = s.toLowerCase();
String sub = sub.toLowerCase();
int pos = s.indexOf(sub);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.