为什么从java中的文件读取int阵列时会引发异常
我正在尝试从文件中读取整数数组。文件格式如下:
893
410
243
264
357
33
793
...
...
我稍后将该数组拆分为4并使用MPI计算它的总和,但我似乎无法读取该文件。我得到这个异常:
java.lang.reflect.InvocationTargetException
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:498)
at runtime.starter.MulticoreStarter$1.run(MulticoreStarter.java:281)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NumberFormatException: For input string: "893"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at lab3.main(lab3.java:28)
... 6 more
Process finished with exit code 0
所以根据我的理解,读取字符串的格式不正确,无法转换为整数,这里是代码:
//create an array of length 400
int[] array = new int[400];
//read the array from the file using the scanner class
try {
Scanner scanner = new Scanner(new File("random1000.txt"));
for (int i = 0; i < array.length; i++) {
array[i] = Integer.parseInt(scanner.nextLine());
}
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}
我还尝试了 scanner.nextInt()
和 Integer.valueof()
而不是 parseInt,这是我在 scanner.nextInt()
中遇到的错误
java.lang.reflect.InvocationTargetException
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:498)
at runtime.starter.MulticoreStarter$1.run(MulticoreStarter.java:281)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
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 lab3.main(lab3.java:30)
... 6 more
这是我第一次使用 Scanner 类,而且我对 java 还很陌生,我做错了什么我该如何解决这个问题?
这是完整的代码,我还没有测试任何 mpi 的东西,但我尝试在没有 mpi 代码的情况下读取文件,并且遇到了同样的问题。
import mpi.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class lab3 {
//Build a program that sums a big array (for example 1000 elements) over four processes
public static void main(String[] args) {
//initialize MPI
MPI.Init(args);
//get the rank of the process
int rank = MPI.COMM_WORLD.Rank();
//if the rank is 0
if (rank == 0) {
// read the array from the file
int[] array = new int[400];
//read the array from the file using file reader
try {
Scanner scanner = new Scanner(new File("random1000.txt"));
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt();
}
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}
//divide the array into 4 parts and send it to the other processes
int[] array1 = new int[array.length / 4];
int[] array2 = new int[array.length / 4];
int[] array3 = new int[array.length / 4];
int[] array4 = new int[array.length / 4];
for (int i = 0; i < array.length / 4; i++) {
array1[i] = array[i];
array2[i] = array[i + array.length / 4];
array3[i] = array[i + array.length / 2];
array4[i] = array[i + array.length / 4 + array.length / 2];
}
//send each part to the other processes
MPI.COMM_WORLD.Send(array2, 0, array2.length, MPI.INT, 1, 0);
MPI.COMM_WORLD.Send(array3, 0, array3.length, MPI.INT, 2, 0);
MPI.COMM_WORLD.Send(array4, 0, array4.length, MPI.INT, 3, 0);
//calculate the sum of the array 1
int sum1 = 0;
for (int i = 0; i < array1.length; i++) {
sum1 += array1[i];
}
//receive the summation from the other processes
int sum2 = 0;
MPI.COMM_WORLD.Recv(sum2 ,0, 0, MPI.INT, 1, 0);
int sum3 = 0;
MPI.COMM_WORLD.Recv(sum3 ,0, 0, MPI.INT, 2, 0);
int sum4 = 0;
MPI.COMM_WORLD.Recv(sum4 ,0, 0, MPI.INT, 3, 0);
//calculate the final sum
int sum = sum1 + sum2 + sum3 + sum4;
//print the final sum
System.out.println("The Final sum is: " + sum);
//finalize MPI
MPI.Finalize();
}
else {
//receive the array from the process 0
int[] array = new int[1000 / 4];
MPI.COMM_WORLD.Recv(array, 0, array.length, MPI.INT, 0, 0);
//calculate the sum of the array
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
//send the sum to the process 0
MPI.COMM_WORLD.Send(sum, 0, 0, MPI.INT, 0, 0);
//print local sum and rank
System.out.println("The sum of the array is: " + sum + " and the rank is: " + rank);
}
}
}
IntelliJ Idea,Windows 11。
编辑:
我尝试这样做:
List<Integer> ints = Files.lines(Paths.get("random1000.txt"))
.map(Integer::parseInt)
.collect(Collectors.toList());
array = ints.stream().mapToInt(i -> i).toArray();
抛出java.lang.NumberFormatException:对于输入字符串:“893”
I am trying to read an integer array from a file. the file format is as follows:
893
410
243
264
357
33
793
...
...
I will later split that array into 4 and calculate it's sum using MPI, but i can't seem to be able to read the file. I get this exception:
java.lang.reflect.InvocationTargetException
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:498)
at runtime.starter.MulticoreStarter$1.run(MulticoreStarter.java:281)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NumberFormatException: For input string: "893"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at lab3.main(lab3.java:28)
... 6 more
Process finished with exit code 0
So from what i understand, the format of the read string is not correct to be cast as an integer, here is the Code:
//create an array of length 400
int[] array = new int[400];
//read the array from the file using the scanner class
try {
Scanner scanner = new Scanner(new File("random1000.txt"));
for (int i = 0; i < array.length; i++) {
array[i] = Integer.parseInt(scanner.nextLine());
}
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}
I also tried scanner.nextInt()
and Integer.valueof()
instead of parseInt, here is the error i got for scanner.nextInt()
java.lang.reflect.InvocationTargetException
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:498)
at runtime.starter.MulticoreStarter$1.run(MulticoreStarter.java:281)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
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 lab3.main(lab3.java:30)
... 6 more
It is my first time using the Scanner class and i am fairly new to java, what am i doing wrong and how do i fix this?
Here is the FULL code, i haven't tested any of the mpi stuff out yet, but i tried reading the file without the mpi code and i had the same problem.
import mpi.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class lab3 {
//Build a program that sums a big array (for example 1000 elements) over four processes
public static void main(String[] args) {
//initialize MPI
MPI.Init(args);
//get the rank of the process
int rank = MPI.COMM_WORLD.Rank();
//if the rank is 0
if (rank == 0) {
// read the array from the file
int[] array = new int[400];
//read the array from the file using file reader
try {
Scanner scanner = new Scanner(new File("random1000.txt"));
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt();
}
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}
//divide the array into 4 parts and send it to the other processes
int[] array1 = new int[array.length / 4];
int[] array2 = new int[array.length / 4];
int[] array3 = new int[array.length / 4];
int[] array4 = new int[array.length / 4];
for (int i = 0; i < array.length / 4; i++) {
array1[i] = array[i];
array2[i] = array[i + array.length / 4];
array3[i] = array[i + array.length / 2];
array4[i] = array[i + array.length / 4 + array.length / 2];
}
//send each part to the other processes
MPI.COMM_WORLD.Send(array2, 0, array2.length, MPI.INT, 1, 0);
MPI.COMM_WORLD.Send(array3, 0, array3.length, MPI.INT, 2, 0);
MPI.COMM_WORLD.Send(array4, 0, array4.length, MPI.INT, 3, 0);
//calculate the sum of the array 1
int sum1 = 0;
for (int i = 0; i < array1.length; i++) {
sum1 += array1[i];
}
//receive the summation from the other processes
int sum2 = 0;
MPI.COMM_WORLD.Recv(sum2 ,0, 0, MPI.INT, 1, 0);
int sum3 = 0;
MPI.COMM_WORLD.Recv(sum3 ,0, 0, MPI.INT, 2, 0);
int sum4 = 0;
MPI.COMM_WORLD.Recv(sum4 ,0, 0, MPI.INT, 3, 0);
//calculate the final sum
int sum = sum1 + sum2 + sum3 + sum4;
//print the final sum
System.out.println("The Final sum is: " + sum);
//finalize MPI
MPI.Finalize();
}
else {
//receive the array from the process 0
int[] array = new int[1000 / 4];
MPI.COMM_WORLD.Recv(array, 0, array.length, MPI.INT, 0, 0);
//calculate the sum of the array
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
//send the sum to the process 0
MPI.COMM_WORLD.Send(sum, 0, 0, MPI.INT, 0, 0);
//print local sum and rank
System.out.println("The sum of the array is: " + sum + " and the rank is: " + rank);
}
}
}
IntelliJ Idea , Windows 11.
EDIT:
i tried doing it this way:
List<Integer> ints = Files.lines(Paths.get("random1000.txt"))
.map(Integer::parseInt)
.collect(Collectors.toList());
array = ints.stream().mapToInt(i -> i).toArray();
throws java.lang.NumberFormatException: For input string: "893"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我创建了一个新文件,并手动添加了一些随机数,据我了解,我用来创建文本文件的随机数生成器可能在开始时添加了一些额外的字节,这可能是问题的原因。
I created a new file and added some random numbers manually and it worked, to my understanding, the random number generator I used to create the text file might have added some extra bytes at the beginning which might be the cause of the problem.