英文:
Reading Copied Integer Values from Excel to .txt file in Java
问题
我想从文本文件中读取整数值到一个数组中。如果在.txt文件中键入数字,代码运行正常。然后,我尝试从Excel复制整数值到文本文件中,并尝试读取它。在这种情况下,我收到以下错误代码。
[989 1042 1059 1067 1087 1098 1103 1110 1114 1131]
Exception in thread "main" java.lang.NumberFormatException: For input string: "989 1042 1059 1067 1087 1098 1103 1110 1114 1131"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at readparetoapprox.readapproxset(readparetoapprox.java:83)
at DomSortepsilon.main(DomSortepsilon.java:11)
File fileText = new File(FileName);
Scanner sc = new Scanner(new BufferedReader(new FileReader(fileText)));
Scanner sc1 = new Scanner(new BufferedReader(new FileReader(fileText)));
String[] line1 = sc1.nextLine().trim().split(" ");
int rows = 2;
int columns = line1.length;
int[][] intarray = new int[rows][columns];
int x = 0;
while (x < 1) {
for (int i = 0; i < intarray.length; i++) {
String[] line = sc.nextLine().trim().split(" ");
System.out.println(Arrays.toString(line));
for (int j = 0; j < line.length; j++) {
paretofront[i][j] = Integer.parseInt(line[j]);
}
}
x++;
}
System.out.println(Arrays.deepToString(intarray));
我真的不知道为什么我收到NumberFormatException错误。
非常感谢您的帮助。
问候,
Martin
英文:
I want to read an integer value from a text file to an array. The code works fine if type the numbers in the .txt file. Afterwards i tried to copy integer values from excel to the text file and tried to read it. In this case i get the following error code.
[989 1042 1059 1067 1087 1098 1103 1110 1114 1131]
Exception in thread "main" java.lang.NumberFormatException: For input string:
"989 1042 1059 1067 1087 1098 1103 1110 1114 1131"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at readparetoapprox.readapproxset(readparetoapprox.java:83)
at DomSortepsilon.main(DomSortepsilon.java:11)
File fileText = new File(FileName);
Scanner sc = new Scanner(new BufferedReader(new FileReader(fileText)));
Scanner sc1 = new Scanner(new BufferedReader(new FileReader(fileText)));
String[] line1 = sc1.nextLine().trim().split(" ");
int rows = 2;
int columns = line1.length;
int [][] intarray= new int[rows][columns];
int x=0;
while(x<1) {
for (int i=0; i<intarray.length; i++) {
String[] line = sc.nextLine().trim().split(" ");
System.out.println(Arrays.toString(line));
for (int j=0; j<line.length; j++) {
paretofront[i][j] = Integer.parseInt(line[j]);
}
}
x++;
}
System.out.println(Arrays.deepToString(intarray));
I really have no idea why I receive the NumberFormatException error.
I´m really thankful for your help.
Greetings,
Martin
专注分享java语言的经验与见解,让所有开发者获益!
评论