【问题标题】:In Java, how to assign the variable number=Integer.parseInt(args[0]) a value if no argument is passed?在Java中,如果没有传递参数,如何为变量 number=Integer.parseInt(args[0]) 赋值?
【发布时间】:2014-07-19 16:45:02
【问题描述】:

我编写了这个简单的程序,它接受一个命令行参数,将其转换为 int 类型,并在控制台上打印从该数字开始直到无穷大的整数。

如果我不传递任何参数,则打印我的异常消息。

public class Infinity
{
    public static void main(String args[])
    {

        try
        {
            n=Integer.parseInt(args[0]);

            while(true)
            {
                System.out.println(n);
                n++;
            }
        }

        catch(Exception ex)
        {
            System.out.println("A number was not entered.");
        }
    }
}

如果我不传递任何参数,程序本身是否会为“n”赋值?像这样:

            n=Integer.parseInt(args[0]);
            if(args[0]==NULL)
            {
                n=0;
            }

【问题讨论】:

  • if(args.length > 0) { n = args[0]; } else { n = default; } 检查 args 长度是否大于 0。如果是,则为任何 arg 赋值。如果没有,请使用默认值
  • 在尝试参考之前先检查长度。

标签: java variables assign parseint


【解决方案1】:

您可以默认将n 的值指定为0 或任何其他值,并使用if(args.length > 0) { 检查是否给出了任何参数。以下是 cmets 的完整示例:

public class Infinity {
    public static void main(String args[]) {

        /*
        Start by assigning your default value to n, here it is 0
        If valid argument is not given, your program runs 
        starting from this value
        */
        int n = 0;

        // If any arguments given, we try to parse it
        if(args.length > 0) {

            try {
                n = Integer.parseInt(args[0]);
            } catch (NumberFormatException e) {
                System.err.println("Argument" + args[0] + " must be an integer.");

                // Program ends
                System.exit(1);
            }

        }

        // All good, n is either default (0) or user entered value
        while(true) {
            System.out.println(n);
            n++;
        }
    }
}

注意:对java不太熟悉的用户,本程序可以运行:

  1. 保存到Infinity.java
  2. 使用 cmd 或终端编写它:javac Infinity.java
  3. 使用:java Infinityjava Infinity 1000(或任何其他值)执行它

干杯。

【讨论】:

    【解决方案2】:

    在尝试访问之前检查args 数组的长度。

    int n = 0;
    if (args.length == 1)
    {
        n = Integer.parseInt(args[0]);
    }
    

    如果您想处理提供的不是有效整数的参数,您仍然可能需要一个 try/catch 块。

    【讨论】:

      【解决方案3】:

      检查是否有 arg,如果有则解析它。否则,请使用您的默认值。

      if (args.length == 0) {
          n = // default value
      }
      else {
          n = Integer.parseInt(args[0]);
      }
      

      【讨论】:

        【解决方案4】:

        是的,有!您可以检查 args[] 的长度,以便当它的 0 即没有传递参数时,分配一个默认值。这是最简单的代码:

        public class Infinity
        {
            public static void main(String args[])
            {
                try{
                if(args.length<1)
                {
                     n=0; //or any other default value
                }
                else
                {
                    n=Integer.parseInt(args[0]);
                }//can also add a case to check in case the no. of arguments exceeds 1.
                 }
                catch(Exception e)
                 {e.printStackTrace();}
                 while(true)
                    {
                        System.out.println(n);
                        n++;
                    }
                }
        
            }
        }
        

        【讨论】:

        • 你没有尝试捕捉解析
        【解决方案5】:
        /**
         *
         * @author salathielgenese
         */
        public class Loader
        {
        
            /**
         * @param args the command line arguments
         */
            public static void main(String[] args)
            {
                // Init «n» to «0» by default. Even if the conversion failed, you'll get you variable to 0
                // But if the conversion goes on, then the value will be updated to args[0]
                int n = 0;
                if (null != args && 0 < args.length)
                {
                    try
                    {
                        n = Integer.parseInt(args[0]);
                    }
                    catch (NumberFormatException e)
                    {
                        System.err.println("Argument" + args[0] + " must be an integer.");
                        System.exit(1);
                    }
                }
                // The rest of your code
            }
        
        }
        

        【讨论】:

        • 这没有意义。更详细地说明你在说什么。另外,我很确定args 在您不设置参数时不为空,或者您会因为尝试调用args.length 而得到NullPointerException(除非您在空检查中进行长度检查)
        • 你没有给出解释。好的答案通常有一个代码示例和该代码如何提供帮助的摘要
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-15
        • 2015-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        • 2012-07-23
        相关资源
        最近更新 更多