我目前正在从文本文件中的值创建二维数组。 例如: 12 23 2342 45 12 6 23 45 54 12 67 2 3 4 5
应该放入一个看似如下的2D数组:
12 23 2342 45 12 6 23 45 0 0 0 0 54 12 0 0 0 0 67 2 3 4 5 0
目前,这是输出:
PROBLEM
null
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at com.company.Class2.readFile(Class2.java:55)
at com.company.Main.main(Main.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
这是我的主要课程:
package com.company;
import java.io.*;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
//Creates new class2 object
Class2 o = new Class2();
//Creates new file object
File file = new File("src/com/company/new.txt");
//Takes in file object as parameter
int[][] array = o.readFile(file);
//prints as an array
System.out.println(Arrays.toString(array));
}
}
这是我的另一课:
package com.company;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Class2 {
public int[][] readFile(File file) {
try {
Scanner scn = new Scanner(file);
Scanner scn2 = new Scanner(file);
//set initial count (of rows) to zero
int maxrows = 0;
//sets initial count (of columns) to zero
int maxcolumns = 0;
// goes through file and counts number of rows to set array parameters
while (scn.hasNextLine()) {
maxrows++;
scn.nextLine();
}
//counts number of columns to set array parameters
BufferedReader lineReader = new BufferedReader(new FileReader(file));
String line;
while ((line = lineReader.readLine()) != null) {
maxcolumns++;
if (!scn2.hasNext()) {
break;
}
int count = 0;
for (int i = 0; i < line.length(); i++) {
if (String.valueOf(line.charAt(i)).equals(" ")) {
//checks if there is white space
} else {
count++;
}
}
}
// create array of counted size
int[][] array = new int[maxrows][maxcolumns];
//new scanner to reset
Scanner scan1 = new Scanner(file);
//places integer one by one into array
for (int row = 0; row < maxrows; row++)
for (int column = 0; column < maxcolumns; column++)
array[row][column] = scan1.nextInt();
return array;
}
// general exception
catch (Exception e) {
System.out.println("PROBLEM");
e.printStackTrace();
//returns null array
return null;
}
}
}
答案 0 :(得分:0)
看起来像txt文件的问题包含的元素少于您正在阅读的元素。 您应该使用以下值填充文本。 12 23 2342 45 12 6 23 45 0 0 0 0 54 12 0 0 0 0 67 2 3 4 5 0
否则你应该知道它何时跳过nextInt()。
答案 1 :(得分:0)
您正在计算maxcolumns
错误。最大列与最大行相同。你做了正确的空格检查。但您没有使用从count
获得的任何信息。
试试这个
while (scn.hasNextLine())
{
maxrows++;
scn.nextLine();
}
int[][] arr = new int[maxrows][];
String[] str_arr = new String[];
int row = 0;
BufferedReader lineReader = new BufferedReader(new FileReader(file));
String line;
while ((line = lineReader.readLine()) != null)
{
row++;
if (!scn2.hasNext())
break;
str_arr = line.split(" ")
maxcolumns = str_arr.length;
for(int i = row - 1; i < row; i++)
{
for (int j = 0; j < maxcolumns; j++)
arr[i][j] = Integer.parseInt(str_arr[j]);
}
}