**JAVA Program- reading in a file and user input into two different String array
ID: 3880676 • Letter: #
Question
**JAVA Program- reading in a file and user input into two different String arrays.
I have a JAVA program that takes in a file and searches for specified words. It is supposed to be a excuatble jar file so that the command promt would be
%(the java program name)(file)(Word1)(word2)(word3)....ect.
I finished most of the program i'm just struggling with reading in the file and the keywords into two different string arrays. So any help with that would be great.
Below is a segemnt of the code that is my attempt at reading them into String arrays, however I run into a null pointer exception.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
in=new Scanner(new File(args[0]));
scnr = new Scanner(System.in);
int i=0;
String []A = null;
while(in.hasNextLine()){
A[i]= in.nextLine();
i++;
}
int k=0;
String[]words = null;
while(scnr.hasNext()) {
words[k]= scnr.next();
k++;
}
Explanation / Answer
String[] A = null
You have declared A to be null. That means A is reference to null. Then you are using A[i] which points to null. Hence, you are getting null pointer exception. So, inorder to use A initialise A like :
String[] A= new String[100]; or use an arraylist if size is not known.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.