Hi I am loading a file into my program and assigning each value (stored on a new line to a array) I cant seem to spot why the array holding the file content is null in each index.
private void readAndProcessWords() {
try {
FileReader _fr = new FileReader(FILEPATH);
BufferedReader textReader = new BufferedReader(_fr);
int numLines = getNumLines(textReader);
String[] words = new String[numLines];
for(int i=0;i<numLines;i++){
words[i] = textReader.readLine();
System.out.println(words[i]);
}
//clears memory reserved for this buffered reader
textReader.close();
} catch(IOException e) {
System.out.println(e);
}
}
private int getNumLines(BufferedReader textReader) throws IOException{
String line;
int numLines =0;
while((line = textReader.readLine()) != null){
numLines++;
}
return numLines;
}
}
Solution: add the below code above the loop to 'reset' the file reader
_fr = new FileReader(FILEPATH);
textReader = new BufferedReader(_fr);
while((line = textReader.readLine()) != null){does?