我需要一個文本文件並從該文件中的文本初始化一個二維數組。文本文件中的每一行都有20個字符。 5行。Java:將文本文件轉換爲二維數組
到目前爲止我只有
int totalRow = 5;
int totalColumn = 20
char[][] myArray = new char[totalRow][totalColumn];
File file = new File("test.txt");
Scanner scanner = new Scanner(file);
int row = 0;
int column = 0;
while (scanner.hasNextLine()){
while (scanner.hasNext()){
myArray[row][column] = scanner.next();
column++;
}
row++;
}
我目前遇到的問題是,我得到錯誤:不能將字符串轉換爲char
另外,我不是很熟悉掃描儀類,所以如果hasNextLine和hasNext不適合我想實現的目標,請告訴我。
http://mattgemmell.com/what-have-you-tried/ – The111
繼續。寫更多的代碼。如果您遇到特定問題 - 請回答確切的問題。 –
足夠公平,我會編輯我的主帖 – TryingToCode