'import txt file to 2d array with scanner
I want to import a textfile to a 2d array with a scanner but can't really get it to work. The name for the textfile should import as an argument to the program.
This is what I got now right now, it prints out the right textfile but I can't get it to store it to the 2d array. And I have no clue how to get the name as an argument. How do I do that?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Maze {
char[][] mazeArray;
private Position startPos;
private boolean[][] visited;
public void main(String[] args) {
int totalRow = getNumRows();
int totalColumn = getNumColumns();
try {
char[][] mazeArray = new char[totalRow][totalColumn];
File f = new File("text.txt");
Scanner myReader = new Scanner(f);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (
FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
System.out.println(Arrays.deepToString(mazeArray));
}
Solution 1:[1]
Your code has a few problems. The main method needs to be declared static. Same for the member variable mazeArray if you want to access it from a static method. This is probably not what you want to do in the long run; you should create an instance of Maze and work with that, making main really small.
Then your code is incomplete, Position, getNumColumns, and getNumRows are missing. There is too much that you want your code to do (get the file name from the command line, fill the maze); it almost looks like you want us to write the code for you. That is why you're getting downvoted: you don't give us the necessary information to help you, and your code doesn't even compile, still you ask about its behavior as if it did compile. Pro tip: a MCVE is really appreciated here.
Once your code compiles, you re-declare mazeArray in the try block. This creates a new separate variable that only lives to the end of that block. When you System.out.println(Arrays.deepToString(mazeArray)); that refers to the mazeArray class member, but that was never initialized. But then again, you never use the mazeArray in the try block either, so you effectively just read the lines in the file, print them, and ignore them.
You could write your code like this:
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Maze {
// make the maze dimension constant (final) and move them outside the
// function, they are probably needed elsewhere as well.
static final int totalRow = 3;
static final int totalColumn = 3;
// declare that main can throw IOException, whuch us a base class for
// FileNotFoundException, so that you don't have to handle it
public static void main(String[] args) throws IOException {
char[][] mazeArray = new char[totalRow][totalColumn];
// take input file name from command line
File f = new File(args[0]);
// Change the try block to be a try-with-resources, so that the Scanner
// gets automatically closed, even when there was an exception.
// Remove the catch block as it didn't do anything useful anyway.
try (Scanner myReader = new Scanner(f)) {
for (int r = 0; r < totalRow; r++) {
for (int c = 0; c < totalColumn; c++) {
// read next token as String, and keep only the first character
String next = myReader.next();
mazeArray[c][r] = next.charAt(0);
}
}
}
// print the maze
System.out.println(Arrays.deepToString(mazeArray));
}
}
This works for an input file like this:
a b c
d e f
g h i
and prints:
[[a, d, g], [b, e, h], [c, f, i]]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Robert |
