I'm supposed to be reading in a text file with numbers that are scores. In the text file it's put like this 88 98 95 45 65 23 etc etc.
This is my code so far below.
import java.io.*; public class ArrayFiles { public static void main(String[] args) throws IOException { File dataFile = new File("G:\\COSC 1337\\Handouts 6-14\\SomeData.txt"); BufferedReader fin = new BufferedReader(new FileReader(dataFile)); final int size = 32; int[] ts = new int[size]; String str; int i = 0; str = fin.readLine(); while(str!=null) { ts[i] =Integer.parseInt(str); i++; str = fin.readLine(); } fin.close(); } }
I understand that I have created a string array with which to put the numbers into, but I keep getting the NumberFormatException error. I just want to take each number and put it into each array box, so that I can then display the scores in rows of three.
EX: 98 95 94
78 43 21
Exception in thread "main" java.lang.NumberFormatException: For input string: "67 64 93 81 92 75 89 81 56 88 71 80 97 58 78 55 74 84 64 72 69 78 87 84 72 33 83 68 62 88 90 75 " at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:458) at java.lang.Integer.parseInt(Integer.java:499) at ArrayFiles.main(ArrayFiles.java:26) Java Result: 1
I don't understand why I'm getting this error. I just want to put the numbers into individual boxes in the array, but not sure where my error is. Any help is appreciated! Thanks!