user530809
user530809

Reputation: 59

Java throws NumberFormatException

I have a text file where first two lines are integers m and n, then there are m lines that each has n pipe-delimited values. I wrote a program that reads the file and creates m*n array with the values from the file, and it worked fine for bajillion times, and then out of sudden, with the same code, with the same file, it threw NumberFormatException while reading the integer from the first line. The whole code is here:

public class Thegame extends JFrame {
    public Integer st;
    public Integer el;
    public String[][] tab;

    public Thegame(String pth)
    {
        setSize(640,480);
        setTitle(pth);
        File file = new File(pth);
        try
        {
            BufferedReader rdr = new BufferedReader(new FileReader(file));
            st = Integer.valueOf(rdr.readLine());
            el = Integer.valueOf(rdr.readLine());
            tab = new String[st][el];
            for(Integer i=0; i<st; i++)
            {
                String lin = rdr.readLine();
                StringTokenizer spl = new StringTokenizer(lin,"|");
                for(Integer j=0; j<el; j++)
                {
                    tab[i][j] = spl.nextToken();
                }                
            }
            rdr.close();            
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, ex);
        }
    }
}

What really worries me is that the same code worked okay before and out of nowhere it turned out to be bad, so I can't even tell what exactly is wrong...

Upvotes: 1

Views: 2762

Answers (5)

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24261

Something must've changed, otherwise it's magic. Possible suspects:

  • the content of the input file has changed. Have you checked it with some kind of hex editor. Does the file start with digits indeed?
  • Java/system locale have changed. What previously was treated as a number, no longer is a number in the new locale. Typical catches with locale are thousands separators (,).

Upvotes: 1

Alb
Alb

Reputation: 3681

Change:

       st = Integer.valueOf(rdr.readLine());
        el = Integer.valueOf(rdr.readLine());

to be

 String input = rdr.readLine();
 try {
     st = Integer.valueOf( input);
 } catch( NumberFormatException  e){
    System.out.println( "exception reading " + input );
 }
 input = rdr.readLine();
 try {
     el = Integer.valueOf(input);
 } catch( NumberFormatException  e){
    System.out.println( "exception reading " + input );
 }

and you'll get your answer of what the problem string is printed out.

Upvotes: 0

mmutilva
mmutilva

Reputation: 18994

If the behavior changed, something did change.

If the code hasn't changed then either:

  • The file being read changed: look for special (non readable) characters with a hex editor. If you have an input file that doesn’t cause the exception, then compare it with the one that does (again, hex comparison).
  • The java environment changed: check for changes in your Java virtual machine configuration, specially the locale.

Upvotes: 0

jmf8241
jmf8241

Reputation: 1

You didn't happen to somehow get some special characters placed before the first line in the file did you? I know I had that issue once and it took me forever to figure out what was going on. I think notepad++ will let you see if that's the case.

Upvotes: 0

jjnguy
jjnguy

Reputation: 138874

The two possible problem lines are:

st = Integer.valueOf(rdr.readLine());
el = Integer.valueOf(rdr.readLine());

You need to make sure that what the reader is reading is actually an Integer.

Try the following small modification:

st = Integer.valueOf(rdr.readLine().trim());
el = Integer.valueOf(rdr.readLine().trim());

If that doesn't fix it, you need to make sure that you are capturing the correct input.

To help debug the problem, it may be helpful to save the input to a string and print it to see what it looks like before trying to parse it:

String stStr = rdr.readLine().trim()
System.out.println(stStr);
st = Integer.valueOf(stStr);
System.out.println(stStr);
String elStr = rdr.readLine().trim();
el = Integer.valueOf(elStr);

If the values that are printed aren't integers, then you are getting the input incorrectly.

Upvotes: 1

Related Questions