从未分离的txt文件中逐个元素获取并放入矩阵
Get element by element from an unseparated txt file and put into a matrix
How it should look my maze game. 这是我的代码。我的文本文件包含未被 space 分隔的 0 和 1。 (33 行,43 列)。我无法将元素(逐个字符)放入矩阵中,返回的列数等于行数。
private Scanner scanner, colReader;
private int rows = 0;
private int columns = 0;
private int i = 0;
private int j = 0;
private String fileName = "C://Users//blidaru//Desktop//Lab.txt";
private String[][] matrix = null;
public Main() throws FileNotFoundException {
try {
scanner = new Scanner(new File(fileName));
}
catch (Exception e) {
System.out.println("Unable to open file");
}
while (scanner.hasNextLine()) {
++rows;
++i;
colReader = new Scanner(scanner.nextLine());
scanner.useDelimiter("");
while (colReader.hasNext()) {
++columns;
++j;
System.out.println(colReader.next() + " ");
}
}
matrix = new String[rows][columns];
System.out.println("Columns " + columns);
System.out.println("Lines " + rows);
scanner = new Scanner(new File(fileName));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < 43; ++j) {
if (scanner.hasNext()) {
matrix[i][j] = scanner.next();
}
}
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < 43; ++j) {
if (scanner.hasNext()) {
System.out.println(matrix[i][j]);
}
}
}
scanner.close();
colReader.close();
}
public static void main(String[] args) throws FileNotFoundException {
new Main();
}
文件Lab.txt
:
1111111111111111111111111111111111111111111
1000000010001000001000000010000000100000001
1010111010101010101111101011111010111111101
1010001010100010100000001010000010000010001
1011101010111110101111111010111111111010111
.... and so on...(33 rows)
假设您已经了解 List
,您应该这样做以避免多次读取文件:
public static void main(String[] args) {
String input = "1111111111111111111111111111111111111111111\n" +
"1000000010001000001000000010000000100000001\n" +
"1010111010101010101111101011111010111111101\n" +
"1010001010100010100000001010000010000010001\n" +
"1011101010111110101111111010111111111010111\n";
String[][] matrix = load(input);
print(matrix);
}
private static String[][] load(String input) {
List<String[]> rows = new ArrayList<>();
try (Scanner sc = new Scanner(input)) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] cols = new String[line.length()];
for (int i = 0; i < cols.length; i++) {
cols[i] = line.substring(i, i + 1);
}
rows.add(cols);
}
}
return rows.toArray(new String[rows.size()][]);
}
private static void print(String[][] matrix) {
System.out.println("Columns " + matrix[0].length);
System.out.println("Lines " + matrix.length);
for (String[] row : matrix) {
for (String col : row) {
System.out.print(col + " ");
}
System.out.println();
}
}
输出
Columns 43
Lines 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1
1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1
1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1
更新
由于数据代表一个迷宫,每个字符实际上是一个布尔值true/false,您可以将二维数组更改为boolean
,并改进打印。
注意:要使其工作,源文件和输出都需要是 UTF-8,所以 Unicode box-drawing characters 工作。
public static void main(String[] args) {
String input = "1111111111111111111111111111111111111111111\n" +
"1000000010001000001000000010000000100000001\n" +
"1010111010101010101111101011111010111111101\n" +
"1010001010100010100000001010000010000010001\n" +
"1011101010111110101111111010111111111010111\n";
boolean[][] maze = load(input);
print(maze);
}
private static boolean[][] load(String input) {
List<boolean[]> maze = new ArrayList<>();
try (Scanner sc = new Scanner(input)) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
boolean[] row = new boolean[line.length()];
for (int i = 0; i < row.length; i++) {
row[i] = (line.charAt(i) != '0');
}
maze.add(row);
}
}
return maze.toArray(new boolean[maze.size()][]);
}
private static void print(boolean[][] maze) {
final String BORDERS = "┌┬│┐" +
"├┼│┤" +
"──•─" +
"└┴│┘";
for (int y = 0; y < maze.length; y++) {
for (int x = 0; x < maze[y].length; x++) {
if (! maze[y][x])
System.out.print(' ');
else {
boolean left = x > 0 && maze[y][x - 1];
boolean up = y > 0 && maze[y - 1][x];
boolean right = x < maze[y].length - 1 && maze[y][x + 1];
boolean down = y < maze.length - 1 && maze[y + 1][x];
int border = (left ? 1 : 0)
| (right ? 0 : 2)
| (up ? 4 : 0)
| (down ? 0 : 8);
System.out.print(BORDERS.charAt(border));
}
}
System.out.println();
}
}
输出
┌───────┬───┬─────┬───────┬───────┬───────┐
│ │ │ │ │ │ │
│ │ ──┐ │ │ │ │ │ └──── │ ├──── │ └───┬── │
│ │ │ │ │ │ │ │ │ │ │ │
│ └── │ │ └───┘ │ ──────┘ │ ────┴──── │ ──┘
How it should look my maze game. 这是我的代码。我的文本文件包含未被 space 分隔的 0 和 1。 (33 行,43 列)。我无法将元素(逐个字符)放入矩阵中,返回的列数等于行数。
private Scanner scanner, colReader;
private int rows = 0;
private int columns = 0;
private int i = 0;
private int j = 0;
private String fileName = "C://Users//blidaru//Desktop//Lab.txt";
private String[][] matrix = null;
public Main() throws FileNotFoundException {
try {
scanner = new Scanner(new File(fileName));
}
catch (Exception e) {
System.out.println("Unable to open file");
}
while (scanner.hasNextLine()) {
++rows;
++i;
colReader = new Scanner(scanner.nextLine());
scanner.useDelimiter("");
while (colReader.hasNext()) {
++columns;
++j;
System.out.println(colReader.next() + " ");
}
}
matrix = new String[rows][columns];
System.out.println("Columns " + columns);
System.out.println("Lines " + rows);
scanner = new Scanner(new File(fileName));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < 43; ++j) {
if (scanner.hasNext()) {
matrix[i][j] = scanner.next();
}
}
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < 43; ++j) {
if (scanner.hasNext()) {
System.out.println(matrix[i][j]);
}
}
}
scanner.close();
colReader.close();
}
public static void main(String[] args) throws FileNotFoundException {
new Main();
}
文件Lab.txt
:
1111111111111111111111111111111111111111111
1000000010001000001000000010000000100000001
1010111010101010101111101011111010111111101
1010001010100010100000001010000010000010001
1011101010111110101111111010111111111010111
.... and so on...(33 rows)
假设您已经了解 List
,您应该这样做以避免多次读取文件:
public static void main(String[] args) {
String input = "1111111111111111111111111111111111111111111\n" +
"1000000010001000001000000010000000100000001\n" +
"1010111010101010101111101011111010111111101\n" +
"1010001010100010100000001010000010000010001\n" +
"1011101010111110101111111010111111111010111\n";
String[][] matrix = load(input);
print(matrix);
}
private static String[][] load(String input) {
List<String[]> rows = new ArrayList<>();
try (Scanner sc = new Scanner(input)) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] cols = new String[line.length()];
for (int i = 0; i < cols.length; i++) {
cols[i] = line.substring(i, i + 1);
}
rows.add(cols);
}
}
return rows.toArray(new String[rows.size()][]);
}
private static void print(String[][] matrix) {
System.out.println("Columns " + matrix[0].length);
System.out.println("Lines " + matrix.length);
for (String[] row : matrix) {
for (String col : row) {
System.out.print(col + " ");
}
System.out.println();
}
}
输出
Columns 43
Lines 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1
1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1
1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1
更新
由于数据代表一个迷宫,每个字符实际上是一个布尔值true/false,您可以将二维数组更改为boolean
,并改进打印。
注意:要使其工作,源文件和输出都需要是 UTF-8,所以 Unicode box-drawing characters 工作。
public static void main(String[] args) {
String input = "1111111111111111111111111111111111111111111\n" +
"1000000010001000001000000010000000100000001\n" +
"1010111010101010101111101011111010111111101\n" +
"1010001010100010100000001010000010000010001\n" +
"1011101010111110101111111010111111111010111\n";
boolean[][] maze = load(input);
print(maze);
}
private static boolean[][] load(String input) {
List<boolean[]> maze = new ArrayList<>();
try (Scanner sc = new Scanner(input)) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
boolean[] row = new boolean[line.length()];
for (int i = 0; i < row.length; i++) {
row[i] = (line.charAt(i) != '0');
}
maze.add(row);
}
}
return maze.toArray(new boolean[maze.size()][]);
}
private static void print(boolean[][] maze) {
final String BORDERS = "┌┬│┐" +
"├┼│┤" +
"──•─" +
"└┴│┘";
for (int y = 0; y < maze.length; y++) {
for (int x = 0; x < maze[y].length; x++) {
if (! maze[y][x])
System.out.print(' ');
else {
boolean left = x > 0 && maze[y][x - 1];
boolean up = y > 0 && maze[y - 1][x];
boolean right = x < maze[y].length - 1 && maze[y][x + 1];
boolean down = y < maze.length - 1 && maze[y + 1][x];
int border = (left ? 1 : 0)
| (right ? 0 : 2)
| (up ? 4 : 0)
| (down ? 0 : 8);
System.out.print(BORDERS.charAt(border));
}
}
System.out.println();
}
}
输出
┌───────┬───┬─────┬───────┬───────┬───────┐
│ │ │ │ │ │ │
│ │ ──┐ │ │ │ │ │ └──── │ ├──── │ └───┬── │
│ │ │ │ │ │ │ │ │ │ │ │
│ └── │ │ └───┘ │ ──────┘ │ ────┴──── │ ──┘