Having trouble reading in a text file with integers..keep getting NumberFormatEx?
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.
Code :
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
Code :
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!
Re: Having trouble reading in a text file with integers..keep getting NumberFormatEx?
Integer.parseInt method will throw a NumberFormatException if it attempts to parse a String that is not a numeral. In this case, you are passing a series of integers. See the API for String for methods that will allow you to parse a line into individual numbers (hint - split the line)
Re: Having trouble reading in a text file with integers..keep getting NumberFormatEx?
I thought about this a few minutes ago, but I'm having trouble implementing it. I checked the Java API and so since my string str is reading in the whole line of numbers into that string, I need to break it at empty white space. I understand that once the string has been read in, you need to split it before you can parse it. I'm trying to put str.split("\\s"); as the first thing in the while loop, but still recieving the same error.
edit:
Code :
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;
String placeHolder[];
int i = 0;
str = fin.readLine();
placeHolder = str.split("");
while(str!=null)
{
ts[i] = Integer.parseInt(placeHolder);
i++;
str = fin.readLine();
}
fin.close();
}
}
Re: Having trouble reading in a text file with integers..keep getting NumberFormatEx?
Use some println's to debug the values you are getting after you split the String. Further, the Exception should tell you (as it does above) the String you are trying to parse - if that String is the same as above, you are still feeding the parseInt the same String, rather than the parsed array of Strings
Re: Having trouble reading in a text file with integers..keep getting NumberFormatEx?
Woohoo! I think I got it! Finally took a closer look at it and realized that my logic had an error in it.
Code :
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 = new String();
str = fin.readLine();
String placeHolder[];
placeHolder = str.split(" ");
int numbers[] = new int[32];
for (int g = 0; g < placeHolder.length; g++) {
numbers[g] = Integer.parseInt(placeHolder[g]);
System.out.println(numbers[g]);
}
fin.close();
}
}
Re: Having trouble reading in a text file with integers..keep getting NumberFormatEx?
Try using the java.util.Scanner class, it can simplify your program.
Re: Having trouble reading in a text file with integers..keep getting NumberFormatEx?
you can use string tokenizer for this program to make it simplify...[:))]