2012-02-26 20 views
5

Tôi đã viết một số mã cho một thách thức interviewstreet.com Mã của tôi đưa ra một NumberFormatExceptionBắt một 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); 
     } 
    } 
    } 
} 

Đây là trường hợp thử nghiệm mà họ sử dụng

3 
1 
2 
3 

Output: 
poor conductor 
1 W L 
1 A L 

Dường như có một dấu cách hoặc thứ gì đó ở cuối mỗi dòng gây ra ngoại 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) 

Điều này đã mất nửa giờ và tôi không biết cách khắc phục điều này. Tiêu diệt niềm vui của sự kiện. Ai đó có thể cho tôi biết tôi đang làm gì sai.

+0

nếu nó một không gian dấu, chỉ cần cắt nó ... sử dụng trim() ... –

+0

Cố gắng sử dụng lớp Scanner thay vì cũng đã làm một số đập đầu tôi chống lại một Tường. – nikhil

+2

Nửa giờ không phải là nhiều thực sự :) – mbatchkarov

Trả lời

14

Integer.parseInt() không thể xử lý các chuỗi không phù hợp với định dạng mong muốn của nó, như bạn đã phát hiện ra. Bạn có thể trim() chuỗi trước khi bạn phân tích cú pháp:

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

Điều này loại bỏ khoảng trống đầu và cuối.

+0

Điều này làm việc, cảm ơn. Tôi đã được ấn tượng rằng parseInt trích xuất phần nguyên từ một chuỗi nhất định, không nên nó tự động bỏ qua dấu cách? – nikhil

+3

@nikhil: Đó là một hiển thị không đúng và [docs for 'parseInt'] (http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt (java.lang) .String)) nêu rõ điều này: * Các ký tự trong chuỗi phải là số thập phân, ngoại trừ chữ số đầu tiên ... * Đặc tả phương thức luôn có thẩm quyền hơn so với dự đoán tốt nhất của bạn. Google phải là tài nguyên đầu tiên hoặc thứ hai của bạn. –

+1

@nikhil Đáng buồn là không. Nó hy vọng tất cả các ký tự là chữ số thập phân, ngoại trừ ký tự đầu tiên mà ký tự ASCII trừ của tôi. – GaryF

1

Bạn cần phải cắt chuỗi

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); 
     } 
    } 
    } 
} 
Các vấn đề liên quan