[英]Reading from a text file to fill a 2d array (in java)
I have a text file that is like this: 我有一个像这样的文本文件:
10, 11.
(0, 0).
(3, 10).
(0, 6), (0, 7), (0, 9),
(1, 1), (1, 2), (1, 4), (1, 7),
(2, 1), (2, 7), (2, 8), (2, 10),
(3, 1), (3, 2), (3, 3), (3, 4), (3, 7),
(4, 0), (4, 6), (4, 9), (4, 10),
(5, 2), (5, 3), (5, 4), (5, 5), (5, 7),
(6, 1), (6, 8), (6, 9),
(7, 1), (7, 2), (7, 3), (7, 6),
(8, 1), (8, 5), (8, 6), (8, 8), (8, 10),
(9, 3), (9, 7).
Where the first line gives the dimensions of the 2 d array, second line should equal "S", third line equal "F", and all other lines equal points that should be "X", and anything not listed should be "." 第一行给出二维数组的尺寸时,第二行应等于“ S”,第三行应等于“ F”,所有其他行应等于“ X”的点,未列出的任何内容均应为“”。
I still have to add more to the code, so any unused variables is for that, I'm just trying to get what I have to function first 我仍然需要在代码中添加更多内容,因此任何未使用的变量都是为此而准备的,我只是在尝试首先获取要运行的功能
So far the error I am getting is: 到目前为止,我得到的错误是:
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at MazeOne.main(MazeOne.java:15)
线程“主”中的异常java.util.Scanner.throwFor(未知源)处的java.util.Scanner.next(未知源)处的java.util.Scanner.nextInt(未知源)处的java.util.InputMismatchException。 MazeOne.main(MazeOne.java:15)的util.Scanner.nextInt(未知源)
When I try to run similar codes without reading from a text file and I print the array it also gives me weird stuff like this instead of the array: 当我尝试运行类似的代码而不读取文本文件时,我打印了数组,这也给了我类似这样的东西,而不是数组:
[[Ljava.lang.String;@55f96302, [Ljava.lang.String;@3d4eac69, [Ljava.lang.String;@42a57993, [Ljava.lang.String;@75b84c92]
My code so far looks like: 到目前为止,我的代码如下:
Any advice or help on where I'm going wrong would be greatly appreciated 任何关于我要去哪里的建议或帮助将不胜感激
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
import java.util.Scanner;
public class MazeOne {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
int HEIGHT, LENGTH;
//Get file name from keyboard
File importFileName = new File("MazeOne.txt");
Scanner inFile = new Scanner("MazeOne.txt");
int Height = inFile.nextInt();
int Length = inFile.nextInt();
HEIGHT = Height-1;
LENGTH = Length-1;
String [][] MazeArray = new String[HEIGHT][LENGTH];
String [][] Start = new String[][]{{".",".","."},{".","S","."},{".",".","."}};
String [][] Block = new String[][]{{"X","X","X"},{"X","X","X"},{"X","X","X"}};
String [][] Finish = new String[][]{{".",".","."},{".","F","."},{".",".","."}};
String [][] Normal = new String[][]{{".",".","."},{".",".","."},{".",".","."}};
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < LENGTH; j++)
{
MazeArray[i][j] = ".";
}
}
if(inFile.hasNextLine())
{
int X = inFile.nextInt();
int Y = inFile.nextInt();
MazeArray[Y][X] = "S";
}
if(inFile.hasNextLine())
{
int X = inFile.nextInt();
int Y = inFile.nextInt();
MazeArray[Y][X] = "F";
}
while(inFile.hasNextLine())
{
while(inFile.hasNextInt()){
int X = inFile.nextInt();
int Y = inFile.nextInt();
MazeArray[Y][X] = "X";
}
}
System.out.print(MazeArray);
}
}
打印阵列时,请使用此Arrays.toString(arrayName)
。
First 第一
Scanner should take with File : 扫描仪应带有File :
Scanner inFile = new Scanner(new File("path\\MazeOne.txt"));
Not a String
like you do : 不是像您这样的
String
:
Scanner inFile = new Scanner("MazeOne.txt");
You already define your File so you can use : 您已经定义了文件,因此可以使用:
Scanner inFile = new Scanner(importFileName);
Second 第二
The default delimiter of Scanner is a space so when you try to use : Scanner的默认定界符是空格,因此当您尝试使用时:
int Height = inFile.nextInt();
int Length = inFile.nextInt();
This can make a problem, because the delimiter is not correct i suggest to remove the comma between your input to be 10 11
这可能会引起问题,因为定界符不正确,我建议删除输入之间的逗号为
10 11
The same problem with the other inputs (0, 0).
其他输入
(0, 0).
有同样的问题(0, 0).
and ... 还有...
I suggest to make your inputs without any special character instead you can use 我建议您输入时不要使用任何特殊字符,而可以使用
10 11
0 0
3 10
0 6 0 7 0 9
1 1 1 2 1 4 1 7
2 1 2 7 2 8 2 10
3 1 3 2 3 3 3 4 3 7
4 0 4 6 4 9 4 10
5 2 5 3 5 4 5 5 5 7
6 1 6 8 6 9
7 1 7 2 7 3 7 6
8 1 8 5 8 6 8 8 8 10
9 3 9 7
Third 第三
You will get java.lang.ArrayIndexOutOfBoundsException: 10
exception because you have 12 line, and you initialize your array with just 10
您将获得
java.lang.ArrayIndexOutOfBoundsException: 10
异常,因为您有12行,并且仅用10
行初始化了数组
Forth 向前
To print your array you need to use loops, or you can use : 要打印数组,您需要使用循环,也可以使用:
Arrays.deepToString(MazeArray)
Note don't use upper letter in the first character of your variables. 注意不要在变量的第一个字符中使用大写字母。
Fix this and you will solve your problem 解决此问题,您将解决您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.