Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取NumberFormatException_Java_Numberformatexception - Fatal编程技术网

Java 获取NumberFormatException

Java 获取NumberFormatException,java,numberformatexception,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));

我在为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);
      }
    }
  }
}
每行末尾似乎都有一个尾随空格或其他导致异常的内容

$ 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)
$java-blindpasser
Integer.parseInt()
无法处理不符合预期格式的字符串,正如您所发现的那样。您可以在解析字符串之前删除它:

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

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

您必须修剪字符串

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);
      }
    }
  }
}
import java.io.*;
公务舱旅客
{
公共静态布尔值isEmpty(最终字符串)
{  
返回字符串==null | | string.trim().isEmpty();
}  
公共静态void main(字符串[]args)引发IOException
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
String line=br.readLine();
int t,n=0;
//系统输出打印项次(行);
t=整数.parseInt(行);

对于(int i=0;ii如果它是一个尾随空间,只需修剪它…使用trim()…尝试使用Scanner类,但也做了一些碰撞。半小时并不是那么长:)这很有效,谢谢。我的印象是parseInt从给定的字符串中提取了整数部分,难道它不应该自动忽略尾随空格吗?@nikhil:这是一个错误的印象,并且清楚地指出:字符串中的字符必须都是十进制数字,除了第一个数字……方法规范是alwa它比你最好的猜测更权威。谷歌应该是你的第一或第二资源。@nikhil遗憾的是不是。它希望所有字符都是十进制数字,除了第一个字符是ASCII减号。@nikhil:API是你的朋友。
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);
      }
    }
  }
}