Java Error

JC724

Weaksauce
Joined
Jan 20, 2016
Messages
118
I am working through some programming interview questions. Got a couple of books to go over. Figured it would help me for interviews in the near future.

I came across a bug really fast, which makes me think I am rusty as the past semester I had mostly theory classes.

I have a text file saved where it has something like this in it. 1 1 1 2 3 3 3 5 5 7 and I am simple trying to read in the file and turn the string values into Ints. But I keep getting this error.

Exception in thread "main" java.lang.NumberFormatException: For input string: "1 1 1 4 4 5 6 6 6 7 8 8 8 8"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at testing.main(testing.java:29)

showing code below

Code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class testing {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Integer> resultCount = new ArrayList<Integer>();
        ArrayList<Integer> sourceNum = new ArrayList<Integer>();
        int value = 0;
       
       
        try {
       
            BufferedReader br = new BufferedReader(new FileReader("D:\\Documents D\\testCase1.txt"));
            // read the first line from the text file
            String fileRead = br.readLine();
       
            while(fileRead != null) {
                String[] eachNumber = fileRead.split("    ");
                for(int i = 0; i < eachNumber.length; i++) {
                    value = Integer.parseInt(eachNumber[i]);
                   
                }
               
                fileRead = br.readLine();
           
            }
        }//end of try catch statement
       
        catch (FileNotFoundException ex)
        {
            System.out.println("file not found");
        }//end of catch statement

        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }//end of catch statement   
    }

}
 
Tried to reduce the number of spaces in the split function to match the number of spaces in the line read from file ... looks like the split not working as the separator is not fitting. Not touched java for over a decade but that is the first thing I would check (under the assumption that split() works with the string as a whole)
 
Last edited:
Put some code in place to output the string you're trying to convert to Int to see exactly what's going into the Int parse method.
 
Code:
String[] eachNumber = fileRead.split("    ");
                                      ^^^^

Like ChristianVirtual suggested, the reason parseInt() is throwing an exception is because your split() argument isn't parsing out the way you probably expect it. Your code asks split() to treat four consecutive spaces as the token delimiter. If you don't have exactly four spaces between each token, then nothing gets parsed. And since you're not parsing everything out cleanly, parseInt() chokes on any string argument containing non-numerics (including whitespace) between the digits.

If one checks the java api reference for String, split() takes a String argument that can also be a regular expression. So you can use this technique to parse via as many delimiters as you like. For example, if you wanted to expand your delimiter to 1-4 spaces, you could do something like this:
Code:
String[] eachNumber = fileRead.split("[ ]{1,4}");
However, if you wanted to parse by any arbitrary number of consecutive whitespace as a delimiter, you could do something like this:
Code:
String[] eachNumber = fileRead.split("\\s+");
If you want to get wild, and parse out anything that resembles a number, treating anything non-numeric in-between as a delimiter:
Code:
String[] eachNumber = fileRead.split("\\D+");
Kind of an extreme approach. Since there are times when you want your software to check that your input file is in the right format, this might not be the wisest approach.
 
Last edited:
Back
Top