2012-02-26 4 views
5

내가 내 코드는 NumberFormatException이 여기은 점점 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); 
     } 
    } 
    } 
} 

에게주는 interviewstreet.com 도전에 대한 몇 가지 코드를 작성했다 그들이 될 것 같다

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) 

이것은 30 분이 넘었습니다.이 문제를 해결하는 방법을 모르겠습니다. 이벤트의 즐거움을 죽이지 않습니까? 누군가 내가 잘못하고있는 것을 말해 줄 수 있습니까?

+0

에게 자사의 경우 후행 공백을 트리밍해야 그냥 잘라 ... 트림()를 사용하여 ... –

+0

대신도에 대해 일부 두드리는 내 머리를 한 스캐너 클래스를 사용하여 시도 벽. – nikhil

+2

반 시간은 그렇게 많이하지 않습니다. – mbatchkarov

답변

14

Integer.parseInt() 당신이 알았 듯이 예상 된 형식에 맞지 않는 문자열은 처리 할 수 ​​없습니다.

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

이것은 선행 및 후행 공백을 제거됩니다 : 당신이 그것을 구문 분석하기 전에 문자열을 trim() 수 있습니다.

+0

감사합니다. parseInt가 지정된 문자열에서 정수 부분을 추출 했으므로 자동으로 후미 공백을 무시하면 안됩니까? – nikhil

+3

@nikhil : 잘못된 인상이고,'parseInt'에 대한 문서 (http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt (java.lang .String))는 다음과 같이 명확하게 명시합니다. * 문자열의 문자는 모두 첫 번째 숫자를 제외한 10 진수 여야합니다. * 메서드 사양은 항상 최상의 추측보다 신뢰할 수 있습니다. Google이 귀하의 첫 번째 또는 두 번째 리소스 여야합니다. –

+1

@nikhil 슬프게도. 그것은 내 문자가 ASCII 마이너스 문자 인 첫 번째 문자를 제외한 모든 문자가 십진수가 될 것으로 예상합니다. – GaryF

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