我有一個文本文件,它是這樣的:從文本文件中讀取,以填補的2D陣列(在Java)
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).
當第一行給出了2 d數組的維數,第二行應該等於「S」,第三行等於「F」,所有其他行等於應該是「X」的點,並且任何未列出的應該是「。」。
我還是要添加更多的代碼,因此任何未使用的變量是對於這一點,我只是試圖讓我先來運作
到目前爲止,我得到的錯誤是:
在線程 「主」 java.util.InputMismatchException在 java.util.Scanner.throwFor(未知來源)在 java.util.Scanner.next(未知來源)在 java.util.Scanner.nextInt異常(Unknown Source) java.util.Scanner.nextInt(Unknown Source)at MazeOne.main(M azeOne.java:15)
當我嘗試不從文本文件中讀取運行類似的代碼,我打印陣列,也給了我怪異的東西,像這樣的,而不是數組:
[[Ljava.lang.String;@55f96302, [Ljava.lang.String;@3d4eac69, [Ljava.lang.String;@42a57993, [Ljava.lang.String;@75b84c92]
到目前爲止我的代碼看起來像:
任何意見或者我要去哪裏錯將極大地幫助讚賞
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);
}
}
你可以編輯你的問題,包括你的文本文件應該看起來如何?將它添加到代碼塊。 – Sedrick
對不起,第一次使用本網站,雖然它是一個文本文件的副本粘貼,就像它在記事本中顯示 – Kenneth
當你的數據是'10,11.' <-a字符串時,你正在嘗試讀入ints。 – Sedrick