Need Help Fast - Please Help in Java ArrayList
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.
Code :
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class arrayl {
public static void main(String args[] ) throws FileNotFoundException
{
System.out.println("Please enter choice ");
Scanner input = new Scanner(System.in);
String search = input.next();
Scanner file = new Scanner(new File("c:\\Documents and Settings\\name\\Desktop\\Book.txt"));
ArrayList<String> test = new ArrayList<>();
while (file.hasNext()){
test.add(file.next());
}
System.out.println(test.indexOf(search));
file.close();
}
}
Re: Need Help Fast - Please Help in Java ArrayList
Recommendations:
- Use a debugger or add some debug statements to your code such as read the file token into a String, and then print it out with a println statement immediately after obtaining it and just before you put it into the arraylist.
- Print out the entire ArrayList contents with a println statement immediately after the while loop.
- Clean up your code. Poorly formatted code is hard for us to understand and hard for you or us to debug.
Re: Need Help Fast - Please Help in Java ArrayList
Code :
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();
}
}
Sorry about that. Sometimes, I format it afterwards, but that is just my test file to make sure that it works. Why would i print out the entire ArrayList contents with a println statement immediately after the while loop when it will only use 1 line of the file. The file has multiple lines and I need it to search entire file...
Re: Need Help Fast - Please Help in Java ArrayList
Also, when you mentioned:
Use a debugger or add some debug statements to your code such as read the file token into a String, and then print it out with a println statement immediately after obtaining it and just before you put it into the arraylist.
Are you saying to use a tokenizer or something? If so, I don't see what the difference is because it is reading the entire file by using the loop. Please explain...
Re: Need Help Fast - Please Help in Java ArrayList
I am recommending that you do these things so you can figure out why your program is not working. You need to do some debugging.
Re: Need Help Fast - Please Help in Java ArrayList
I have been debugging. I have been using system.out.println to see what the file is reading and that is how i kow it is reading the entire file. However, it looks as if it is just reading line by line, instead of all the elements within the line. I've also come to find out that ArrayList only searches for objects and that is why it keeps saying it is false. How do I convert my String search, which is the users input to an object?
Re: Need Help Fast - Please Help in Java ArrayList
Strings *are* objects, and so your search may be correct. I suggest that you:
1) post the contents of the text file.
2) put in the println I was mentioning and show us the results from that.
3) show us what the user enters.