I am trying to read a text file and create an array. Setting the array with the first 2 values of the file which appear on the first line. Then adding all remaining items in file to the array. My array is also always null for some reason. What corrections would need to be made for this to function properly?
public static Grid createGrid(String filename) throws IOException {
BufferedReader input;
String inputLine;
char[][] grid;
Grid currentGrid;
int currentLine = 0;
try{
input = new BufferedReader(new FileReader(filename));
inputLine = input.readLine();
String[] tokens = inputLine.split(" ");
if(tokens.length != 2){
throw new IllegalArgumentException();
}
int height = Integer.parseInt(tokens[0]);
int width = Integer.parseInt(tokens[1]);
grid = new char[height][width];
while(inputLine != null){ // Giving me (Condition 'inputLine != null' is always 'true') in ide
System.out.println(inputLine);
inputLine = input.readLine();
for(int i = 0; i < inputLine.length() - 1; i++){
char currentGem = inputLine.charAt(i);
grid[currentLine][i] = currentGem;
}
currentLine++;
}
input.close();
currentGrid = new Grid(grid);
}
catch(IOException e){
System.out.println(e.getMessage());
currentGrid = null;
}
return currentGrid; // Giving me (Value 'currentGrid' is always 'null') in ide
}
In your code...
while(inputLine != null)
... will always be true
because inputLine
is your String
and it will always have a value when this check is run. The reason is because once you read a null
into inputLine
(when you reach the end of your file), instead of running the loop check, you'll have a NullPointerException
thrown the moment you try run the next line of code... for(int i = 0; i < inputLine.length() - 1; i++)
. This in turn will result in currentGrid
always being null
as it's still declared as null
at that point.
To fix this you simply need to add a inputLine = input.readLine();
before your while
and then move that same line in your while
to the end of the while
, as follows...
public static Grid createGrid(String filename) throws IOException {
Grid currentGrid = null;
try {
BufferedReader input = new BufferedReader(new FileReader(filename));
String inputLine = input.readLine();
String[] tokens = inputLine.split(" ");
if(tokens.length != 2){
throw new IllegalArgumentException();
}
int height = Integer.parseInt(tokens[0]);
int width = Integer.parseInt(tokens[1]);
char[][] grid = new char[height][width];
int currentLine = 0;
inputLine = input.readLine(); // added before while
while(inputLine != null) {
System.out.println(inputLine);
for(int i = 0; i < inputLine.length() - 1; i++){
char currentGem = inputLine.charAt(i);
grid[currentLine][i] = currentGem;
}
currentLine++;
inputLine = input.readLine(); // last line in while
}
input.close();
currentGrid = new Grid(grid);
}
catch(IOException e) {
System.out.println(e.getMessage());
currentGrid = null;
}
return currentGrid;
}
Oh, you'll also noticed I moved your declarations around. You do not been to declare BufferedReader input
, String inputLine
and char[][] grid
outside your try {... } catch
block. Also you only need to declare int currentLine = 0;
just before you need it (it's just good practice).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.