Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Having trouble reading in a text file with integers..keep getting NumberFormatEx?

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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!
    Last edited by orbin; June 17th, 2012 at 04:33 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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();
     
     
     
        }
    }
    Last edited by orbin; June 17th, 2012 at 05:22 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  5. #5
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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();
     
     
     
        }
    }
    Last edited by orbin; June 17th, 2012 at 06:15 PM.

  6. #6
    Junior Member
    Join Date
    May 2012
    Location
    Bali, ID
    Posts
    6
    My Mood
    Happy
    Thanks
    0
    Thanked 3 Times in 3 Posts

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

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...[]

Similar Threads

  1. Reading text file and counting the number of words, integers, and floats.
    By Jsmooth in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: April 12th, 2012, 06:39 PM
  2. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  3. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  4. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM
  5. [SOLVED] Reading integers from a file, but it's only displaying the first number repeatedly..?
    By jessica202 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 29th, 2011, 08:16 AM