I took a text file and put it in one array. However, I want to search for a String (word) within the array. In my case I wanted to use the scanner to get input from the user, then search for a string match in the array. How can one achieve such a task?
public static void main(String[]args) throws IOException
{
//Scanner scan = new Scanner(System.in);
//String stringSearch = scan.nextLine();
List<String> words = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));
String line;
while ((line = reader.readLine()) != null) {
words.add(line);
}
reader.close();
System.out.println(words);
}
My output:
[CHAPTER I. Down the Rabbit-Hole, Alice was beginning to get very tired of sitting by her sister on the, bank, and of having nothing to do:]
You can use the .contains() function from List. It will use the .equals() method of the parameter on each element
Just realized you are saving one line per index, and not one word. In this case: