I'm trying to write a code that will read in a keyword and files from the command line and search for that word in the specified files. I am not sure if it works, because I'm getting this exception and I'm not sure how to fix it. It's due within the next two hours, and I really need help just fixing it up and making sure it complies (and hopefully works!) I'm getting the error at line25 in the test and line 20 in the SearchFile (the keyword stuff)... I SO appreciate the help anyone can give me!
Here is my SearchFile class:
import java.util.ArrayList; public class SearchFile { private ArrayList <String> fileContent; public String specLine, lineNumber, fileName; int wordFoundAt; public SearchFile(){ fileContent = new ArrayList <String> (); } public void addLine(String newLine){ fileContent.add(newLine); } public void findWord(String keyWord){ for(int i = 0; i < fileContent.size(); i++){ wordFoundAt = specLine.indexOf(keyWord); } } public String getLine(){ String lineContent = fileContent.get(wordFoundAt); return lineContent; } public String toString(){ String wordFound; getLine(); if (wordFoundAt == - 1){ System.out.println("Sorry - that word could not be found"); }else{ wordFound = fileName + ":" + wordFoundAt + "\n"; } wordFound = fileName + ":" + lineNumber + "\n"; return wordFound; } } And here is my test class: //import java.util.ArrayList; import java.util.Scanner; import java.io.*; public class SearchFilesTest { public static void main(String[] args) { boolean again=true; while(again){ try { File inFile = new File(args[1]); //the file to be used is first arg Scanner input = new Scanner(inFile); for(int noFiles = 0; noFiles < args.length; noFiles++) { SearchFile newSearch = new SearchFile(); String keyWord = (args[0]); while(input.hasNext()) { String newLine = input.nextLine(); newSearch.addLine(newLine); }//end of while newSearch.findWord(keyWord); System.out.println(newSearch.toString()); again=false; }//for }//end try catch(IOException e){ System.out.println("Please provide a correct file name"); Scanner scan = new Scanner(System.in); args[0]=scan.next(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Please provde a file in the command line"); again=false; } }//end while again = true loop } //end main method }