Reputation: 31
I am very confused. I am trying to get 10 integers on 10 lines of a text file into an int[]. I have tried a few different ways, but my latest attempt is to use a for loop and parseInt on the BufferedReader.readLine() for each line of the file. This is returning a NumberFormatException every time.
public class InversionCounter {
static int[] fileToArray(String filename) {
File file = new File(filename);
try ( BufferedReader br = new BufferedReader(new FileReader(file))) {
int numOfLine = Integer.parseInt(br.readLine());
int[] ourArray = new int[10];
for (int i = 0; i < ourArray.length; i++) {
ourArray[i] = numOfLine;
numOfLine = Integer.parseInt(br.readLine());
}
return ourArray;
} catch (NumberFormatException e) {
System.out.println("NumberFormatException");
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("IO Exception");
}
return null;
}
public static void main(String[] args) throws IOException {
fileToArray("/home/paris/coolfile");
}
}
Error:
Exception in thread "main" java.lang.NumberFormatException: null
at java.base/java.lang.Integer.parseInt(Integer.java:614)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at InversionCounter.fileToArray(InversionCounter.java:16)
at InversionCounter.main(InversionCounter.java:30)
The file is simply this:
45
67
87
76
7
4
5
23
5675
3
Upvotes: 0
Views: 759
Reputation: 1037
There are only 10 lines in the file but your readLine pointer is trying to read 11th line. This is happening because you read one line outside of the loop and now trying to read 10 lines in the loop. Following code should work:
public class InversionCounter {
static int[] fileToArray(String filename) {
File file = new File(filename);
try ( BufferedReader br = new BufferedReader(new FileReader(file))) {
int numOfLine = Integer.parseInt(br.readLine());
int[] ourArray = new int[10];
for (int i = 0; i < (ourArray.length - 1); i++) {
ourArray[i] = numOfLine;
numOfLine = Integer.parseInt(br.readLine());
}
return ourArray;
} catch (NumberFormatException e) {
System.out.println("NumberFormatException");
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("IO Exception");
}
return null;
}
public static void main(String[] args) throws IOException {
fileToArray("/home/paris/coolfile");
}
}
P.S: Also, you should trim the line in case if there are any extra spaces.
Upvotes: 1
Reputation: 425013
The problem occurs when the line of the file has been read here:
int numOfLine = Integer.parseInt(br.readLine());
Quoting the javadoc of BufferedReader.readLine()
Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached without reading any characters.
You need to test if the line read is null before parsing it, otherwise Integer.parseInt()
will throw a NumberFormatException
.
This is a simple fix:
String line = br.readLine();
if (line == null) {
break;
}
int numOfLine = Integer.parseInt(line);
A better approach is to not assume a certain quantity of input and just keep reading lines until the end of file (ie until line is null), adding them to a List<Integer>
.
Upvotes: 1