获取NumberFormatException

时间:2012-02-26 16:58:03

标签: java numberformatexception

我正在为interviewstreet.com挑战编写一些代码 我的代码给出了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);
      }
    }
  }
}

以下是他们使用的测试用例

3 
1 
2 
3 

Output: 
poor conductor 
1 W L 
1 A L

在每一行的末尾似乎有一个尾随空格或某些东西导致异常。

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

这花了半个小时,我不知道如何解决这个问题。 杀死事件的乐趣不是吗。有人能告诉我我做错了什么。

2 个答案:

答案 0 :(得分:14)

正如您所知,

Integer.parseInt()无法处理不符合其预期格式的字符串。在解析之前你可以trim()字符串:

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

这消除了前导和尾随空格。

答案 1 :(得分:1)

你必须修剪字符串

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);
      }
    }
  }
}