Skip to content
Advertisement

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

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:

JavaScript

So from what i understand, the format of the read string is not correct to be cast as an integer, here is the Code:

JavaScript

I also tried scanner.nextInt() and Integer.valueof() instead of parseInt, here is the error i got for scanner.nextInt()

JavaScript

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.

JavaScript

IntelliJ Idea , Windows 11.

EDIT:

i tried doing it this way:

JavaScript

throws java.lang.NumberFormatException: For input string: "893"

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement