java.util.scanner 有字符
java.util.scanner with chars
我正在尝试使用面向对象编程创建迷宫查找算法。我将迷宫存储在一个文件中,我希望该文件在解决迷宫之前读取迷宫并打印出来(我知道用其他方式更容易,但我必须使用文件 reader)。但是,我的 charAt 函数不起作用,我不确定如何让 fileReader 读取字符。如果有人能提供一个很棒的解决方案:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static Maze maze;
public static void main(String[] args) {
ArrayList<char[]> mazeArray = new ArrayList<char[]>();
try {
File myMaze = new File("maze.txt");
Scanner fileReader = new Scanner(myMaze);
while (fileReader.hasNextLine()) {
char[] mazeLayout = new char[(int) myMaze.length()];
for (int i = 0; i < Integer.MAX_VALUE; i++){
for (int j = 1; j < myMaze.length(); j++){
mazeLayout[i] = fileReader.next().charAt(j);
mazeArray.add(mazeLayout);
}
}
}
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("File does not exist.");
e.printStackTrace();
}
maze = new Maze(mazeArray);
maze.print();
}
}
拥有示例文件数据以查看正在读取的内容总是很好,因为在这一点上......我只会猜测。如果文件内容是这样的:
xx xxxxxxx
xx xxxxxx
xxx xxxxxx
xxxx xx
xxxxx x xx
xxxx x x
xxxx xxx x
xxxxxx x
xxxxx xxx
xxxxx xxxx
那么你不需要 char[] 数组,你可以将每一行存储到 ArrayList 中,然后它将是一个字符串的 ArrayList (ArrayList<String>
)。现在只需将数组列表中的每个元素打印到控制台 window。我只是不知道,所以......我们只是 假设 就像上面的一样,但我们会把每一行都转换成一个 char[] 数组,因为你似乎想用角色来做这个。考虑到这一点...
需要做的事情:
- 逐行读取文件。
- 读取每一行并将其转换为
char
数组。
- 获取此数组并将其添加到
mazeArray
ArrayList。
- 将 ArrayList (
mazeArray
) 的内容打印到控制台
window.
// ArrayList to hold the Maze from file.
ArrayList<char[]> mazeArray = new ArrayList<>();
// `Try With Resources` used here to auto-close reader and free resources.
try (Scanner fileReader = new Scanner(new File("maze.txt"))) {
// String variable to hold each line read in
String line;
// Loop through the whole file line by line...
while (fileReader.hasNextLine()) {
// Task 1: Apply current line read to the `line` variable.
line = fileReader.nextLine().trim(); // Remove any leading/trailing spaces as well.
// Make sure the line isn't blank (if any). You don't want those.
if (line.isEmpty()) {
continue; // Go to the top of loop and read in next line.
}
// Task #2: Convert the String in the variable `line` to a char[] array.
char[] chars = line.toCharArray();
// Task #3: Add the `chars` character array to the `mazeArray` ArrayList.
mazeArray.add(chars);
}
}
catch (FileNotFoundException ex) {
Logger.getLogger(yourClassName.class.getName()).log(Level.SEVERE, null, ex);
}
/* Task #4: Print the maze contained within the `mazeArray` ArrayList
to the Console Window. */
// Outer loop to get each char[] array within the ArrayList.
for (char[] characters : mazeArray) {
// Inner loop to get each character within the current char[] array.
for (char c : characters) {
System.out.print(c); //Print each character on the same console row.
}
System.out.println(); // Start a new line in console.
}
我正在尝试使用面向对象编程创建迷宫查找算法。我将迷宫存储在一个文件中,我希望该文件在解决迷宫之前读取迷宫并打印出来(我知道用其他方式更容易,但我必须使用文件 reader)。但是,我的 charAt 函数不起作用,我不确定如何让 fileReader 读取字符。如果有人能提供一个很棒的解决方案:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static Maze maze;
public static void main(String[] args) {
ArrayList<char[]> mazeArray = new ArrayList<char[]>();
try {
File myMaze = new File("maze.txt");
Scanner fileReader = new Scanner(myMaze);
while (fileReader.hasNextLine()) {
char[] mazeLayout = new char[(int) myMaze.length()];
for (int i = 0; i < Integer.MAX_VALUE; i++){
for (int j = 1; j < myMaze.length(); j++){
mazeLayout[i] = fileReader.next().charAt(j);
mazeArray.add(mazeLayout);
}
}
}
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("File does not exist.");
e.printStackTrace();
}
maze = new Maze(mazeArray);
maze.print();
}
}
拥有示例文件数据以查看正在读取的内容总是很好,因为在这一点上......我只会猜测。如果文件内容是这样的:
xx xxxxxxx
xx xxxxxx
xxx xxxxxx
xxxx xx
xxxxx x xx
xxxx x x
xxxx xxx x
xxxxxx x
xxxxx xxx
xxxxx xxxx
那么你不需要 char[] 数组,你可以将每一行存储到 ArrayList 中,然后它将是一个字符串的 ArrayList (ArrayList<String>
)。现在只需将数组列表中的每个元素打印到控制台 window。我只是不知道,所以......我们只是 假设 就像上面的一样,但我们会把每一行都转换成一个 char[] 数组,因为你似乎想用角色来做这个。考虑到这一点...
需要做的事情:
- 逐行读取文件。
- 读取每一行并将其转换为
char
数组。 - 获取此数组并将其添加到
mazeArray
ArrayList。 - 将 ArrayList (
mazeArray
) 的内容打印到控制台 window.
// ArrayList to hold the Maze from file.
ArrayList<char[]> mazeArray = new ArrayList<>();
// `Try With Resources` used here to auto-close reader and free resources.
try (Scanner fileReader = new Scanner(new File("maze.txt"))) {
// String variable to hold each line read in
String line;
// Loop through the whole file line by line...
while (fileReader.hasNextLine()) {
// Task 1: Apply current line read to the `line` variable.
line = fileReader.nextLine().trim(); // Remove any leading/trailing spaces as well.
// Make sure the line isn't blank (if any). You don't want those.
if (line.isEmpty()) {
continue; // Go to the top of loop and read in next line.
}
// Task #2: Convert the String in the variable `line` to a char[] array.
char[] chars = line.toCharArray();
// Task #3: Add the `chars` character array to the `mazeArray` ArrayList.
mazeArray.add(chars);
}
}
catch (FileNotFoundException ex) {
Logger.getLogger(yourClassName.class.getName()).log(Level.SEVERE, null, ex);
}
/* Task #4: Print the maze contained within the `mazeArray` ArrayList
to the Console Window. */
// Outer loop to get each char[] array within the ArrayList.
for (char[] characters : mazeArray) {
// Inner loop to get each character within the current char[] array.
for (char c : characters) {
System.out.print(c); //Print each character on the same console row.
}
System.out.println(); // Start a new line in console.
}