When i run the code it gives an error
java.lang.ArrayIndexOutOfBoundsException: 0
at SearchFile.main(SearchFile.java:28)
MyNote.txt is the text file saved in D directory of my computer. whereas "ad" is the word in that text file.
import java.io.*;
public class SearchFile {
public static void main(String args[]) {
args[0] = "ad";
if (args.length > 0) {
String searchword = args[0];
try {
int LineCount = 0;
String line = "";
BufferedReader bReader = new BufferedReader(new FileReader("D:/MyNote.txt"));
while ((line = bReader.readLine()) != null) {
LineCount++;
int posFound = line.indexOf(searchword);
if (posFound > - 1) {
System.out.println("Search word found at position " + posFound + " on line " + LineCount);
}
}
bReader.close();
}
catch (IOException e) {
System.out.println("Error: " + e.toString());
}
}
else {
System.out.println("Please provide a word to search the file for.");
}
}
}
i dont know what the error is or what i have done wrong. i am new to this actually please help!! THANK YOU