为什么从 java 中的文件读取 int 数组时抛出异常

Why does it throw an exception when reading int array from file in java

我正在尝试从文件中读取整数数组。文件格式如下:

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.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.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

这是我第一次使用扫描仪 class,我对 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 创意,Windows11.

编辑:

我试过这样做:

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: For input string: "893"

我创建了一个新文件并手动添加了一些随机数,它起作用了,据我所知,我用来创建文本文件的随机数生成器可能在开头添加了一些额外的字节,这可能是导致问题。