Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.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_Command Line Arguments - Fatal编程技术网

Java 发生NumberFormatException时如何打印?

Java 发生NumberFormatException时如何打印?,java,command-line-arguments,Java,Command Line Arguments,当命令行参数不是整数且出现NumberFormatException异常时,如何打印 我的程序接受3个命令行参数,并根据它们是什么打印某些文本 代码如下: public class CommandLine { public static void main(String[] args) { if(args.length !=3){ System.out.println("Error. Must give 3 values"); }

当命令行参数不是整数且出现
NumberFormatException
异常时,如何打印

我的程序接受3个命令行参数,并根据它们是什么打印某些文本

代码如下:

    public class CommandLine {

  public static void main(String[] args) {

      if(args.length !=3){
          System.out.println("Error. Must give 3 values");
        }
     int x = Integer.parseInt(args[0]);
     int y = Integer.parseInt(args[1]);
     int z = Integer.parseInt(args[2]);

     if((x%2)==0 && (y%2)==0 &&(z%2)==0)
     System.out.println("Even");

else
    System.out.println("odd");

  }

}

您可以捕获该异常并打印:

int x=y=z=Integer.MIN_VALUE;
try{
   x = Integer.parseInt(args[0]);
   y = Integer.parseInt(args[1]);
   z = Integer.parseInt(args[2]);
}catch (NumberFormatException e) {
   System.out.println("x:" +x + " y:" +y +" z:" +z); 
   e.printStackTrace();
}

第一个值仍然是
Integer.MIN\u value
导致了您的异常(除非您的数字是
Integer.MIN\u value

您可以捕获该异常并打印:

int x=y=z=Integer.MIN_VALUE;
try{
   x = Integer.parseInt(args[0]);
   y = Integer.parseInt(args[1]);
   z = Integer.parseInt(args[2]);
}catch (NumberFormatException e) {
   System.out.println("x:" +x + " y:" +y +" z:" +z); 
   e.printStackTrace();
}
第一个值仍然是
Integer.MIN\u value
导致了您的异常(除非您的数字是
Integer.MIN\u value

试试这个

     public class CommandLine {

      public static void main(String[] args) {

          if(args.length !=3){
              System.out.println("Error. Must give 3 values");
            }
         try{
         int x = Integer.parseInt(args[0]);
         int y = Integer.parseInt(args[1]);
         int z = Integer.parseInt(args[2]);

         if((x%2)==0 && (y%2)==0 &&(z%2)==0)
         System.out.println("Even");

    else
        System.out.println("odd");


    }catch(Exception e){
     System.out.println("Exception Caught !");
    }
    }
}
试试这个

     public class CommandLine {

      public static void main(String[] args) {

          if(args.length !=3){
              System.out.println("Error. Must give 3 values");
            }
         try{
         int x = Integer.parseInt(args[0]);
         int y = Integer.parseInt(args[1]);
         int z = Integer.parseInt(args[2]);

         if((x%2)==0 && (y%2)==0 &&(z%2)==0)
         System.out.println("Even");

    else
        System.out.println("odd");


    }catch(Exception e){
     System.out.println("Exception Caught !");
    }
    }
}

您需要捕获或抛出异常。请参见第3.7.2节:。它显示了一个处理
NumberFormatException
的示例。使用try catch处理异常捕获
NumberFormatException
,或者在解析参数之前测试参数的内容。您需要捕获异常或抛出异常。请参见第3.7.2节:。它显示了一个处理
NumberFormatException
的示例。使用try catch处理异常捕获
NumberFormatException
,或者在解析参数之前测试参数的内容。我不完全确定您在这里使用
Integer.MIN\u VALUE
做了什么。它不在OP的代码中,你用它做过什么实验吗?我只是想让OP通过查看x,y,z的值来识别哪个变量导致了错误。我认为这样做会有助于OP而不是查看堆栈跟踪。我不完全确定您在这里使用
Integer.MIN\u VALUE
做了什么。它不在OP的代码中,你用它做过什么实验吗?我只是想让OP通过查看x,y,z的值来识别哪个变量导致了错误。我认为这将有助于OP的这种方式,而不是看堆栈跟踪。