Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting a string array into an int array  RSS feed

 
Nikki Harding
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm writing a program that uses a bunch of different methods to grade tests taken from a file. I have read in this file, split the pieces apart, and now have a string array. I want to take only the ID numbers and put them into an array of their own, but I keep getting an error.

The text file looks like this:
819283761, 2112211241
834987341, 2221213241
899991828, 1122113241
888181818, 2121113131
892823736, 1111111111

The error: java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)







 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general String array can be converted to int array by using Integer.parseInt() method.
Traverse the string array using for each loop and then use Integer.parseInt() on each individual string to save the output in an int array.



I'm sure there must be other ways but this is pretty easy.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you shouldn't read Strings with a Scanner; you should read ints directly. Use the nextInt method.

The real problem is that you are continuing to read beyond the end of the file. You haven't shown us how you are reading the file. You have however shown two serious errors, the large main method and the fact that you have used the keyword static elsewhere, which suggests you don't know what static means.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shubham Semwal wrote:. . . I'm sure there must be other ways but this is pretty easy.
If you are going to use a loop to populate an array, then use a for loop not a for‑each loop.
Since this is the beginning forum please explain how [cnt++] works. Why did you write cnt rather than count?
If you are going to convert a String[] to int[] use a Stream:-The Arrays#stream() method creates a sequential (=ordinary) Stream, well actually Stream<T> which means a Stream<String>. As that Stream iterates itself, it creates a second Stream, actually an IntStream, using the mapToInt() method. That takes a ToIntFunction reference which converts things to ints. By passing Integer::parseInt you are telling the compiler to create a ToIntFunction which uses the parseInt() method. You can only use the shorthand method (creating a λ) because ToIntFunction is an interface with one abstract method. Finally the toArray() method creates an int[].
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you are going to convert a String[] to int[] use a Stream...

And to convert a File to "words", you can use:However, if you've already gone that far, you might as well complete the process and convert them to an int[].

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!