Getting a NumberFormatException
Asked Answered
S

2

5

I was writing some code for an interviewstreet.com challenge My code gives a NumberFormatException

import java.io.*;

public class BlindPassenger
{
  public static void main(String [] args) throws IOException
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();
    int t,n;
    //System.out.println(line);
    t = Integer.parseInt(line);
    for(int i=0;i<t;++i)
    {
      line = br.readLine();
      n = Integer.parseInt(line); --n;
      if(n == 0)
      {
        System.out.println("poor conductor");
      }
      else
      {
        char direction='l',seat_posn='l';
        int row_no = 0, relative_seat_no = 0;
        row_no = (int) Math.ceil(n/5.0);
        relative_seat_no = n % 5;
        if(row_no % 2 == 0)
        {
          //even row, need to reverse the relative seat no
          relative_seat_no = 6 - relative_seat_no;
        }

        if(relative_seat_no < 3)
        {
          direction = 'L';
          if(relative_seat_no == 1) seat_posn = 'W';
          else seat_posn = 'A';
        }
        else
        {
          direction = 'R';
          if(relative_seat_no == 3) seat_posn = 'A';
          else if(relative_seat_no == 4) seat_posn = 'M';
          else seat_posn = 'W';
        }

        System.out.println(row_no + " " + seat_posn + " " + direction);
      }
    }
  }
}

Here is the test case that they use

3 
1 
2 
3 

Output: 
poor conductor 
1 W L 
1 A L

There seems to be a trailing space or something at the end of each line that causes the exception.

$ java BlindPassenger <input00.txt
Exception in thread "main" java.lang.NumberFormatException: For input string: "3
 "
        at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:65)
        at java.lang.Integer.parseInt(Integer.java:492)
        at java.lang.Integer.parseInt(Integer.java:527)
        at BlindPassenger.main(BlindPassenger.java:11)

This has taken up half an hour and I don't know how to fix this. Kills the fun of the event doesn't it. Can someone tell me what I'm doing wrong.

Shrewd answered 26/2, 2012 at 16:58 Comment(2)
if its a trailing space, just trim it...using trim()...Karyolysis
Tried using the Scanner class instead also did some banging my head against a wall.Shrewd
I
14

Integer.parseInt() can't handle strings that don't fit its expected format, as you've found out. You could trim() the string before you parse it:

t = Integer.parseInt(line.trim());

This gets rid of leading and trailing whitespace.

Illstarred answered 26/2, 2012 at 17:0 Comment(4)
This worked, thanks. I was under the impression that parseInt extracted the integer part from a given string, shouldn't it automatically ignore the trailing space?Shrewd
@nikhil: That's an incorrect impression, and the docs for parseInt clearly state this: The characters in the string must all be decimal digits, except the first digit... The method specification is always more authoritative than your best guess. Google should be your first or second resource.Illstarred
@Shrewd Sadly not. It expects all characters to be decimal digits, except for the first character which my an ASCII minus character.Hospitable
@nikhil: The API is your friend.Bandoline
E
1

You have to trim the string

import java.io.*;

public class BlindPassenger
{


    public static boolean isEmpty(final String string)  
            {  
               return string == null  || string.trim().isEmpty();  
            }  
  public static void main(String [] args) throws IOException
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();
    int t,n=0;
    //System.out.println(line);
    t = Integer.parseInt(line);
    for(int i=0;i<t;++i)
    {
      line = br.readLine();

     if(!isEmpty(line)){
      n = Integer.parseInt(line.trim());
      --n;
     }

      if(n == 0)
      {
        System.out.println("poor conductor");
      }
      else
      {
        char direction='l',seat_posn='l';
        int row_no = 0, relative_seat_no = 0;
        row_no = (int) Math.ceil(n/5.0);
        relative_seat_no = n % 5;
        if(row_no % 2 == 0)
        {
          //even row, need to reverse the relative seat no
          relative_seat_no = 6 - relative_seat_no;
        }

        if(relative_seat_no < 3)
        {
          direction = 'L';
          if(relative_seat_no == 1) seat_posn = 'W';
          else seat_posn = 'A';
        }
        else
        {
          direction = 'R';
          if(relative_seat_no == 3) seat_posn = 'A';
          else if(relative_seat_no == 4) seat_posn = 'M';
          else seat_posn = 'W';
        }

        System.out.println(row_no + " " + seat_posn + " " + direction);
      }
    }
  }
}
Exposure answered 13/5, 2013 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.