在Java中,如果没有传递参数,如何为变量number = Integer.parseInt(args [0])赋值?

时间:2014-07-19 16:45:03

标签: java variables assign parseint

我编写了这个简单的程序,它接受命令行参数,将其转换为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;
            }

5 个答案:

答案 0 :(得分:3)

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

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. 通过编写:javac Infinity.java
  3. ,使用cmd或终端进行编译
  4. 执行:java Infinityjava Infinity 1000(或任何其他值)
  5. 干杯。

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

检查你是否有arg,然后解析它。否则,请使用默认值。

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

答案 3 :(得分:0)

是的!您可以检查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++;
            }
        }

    }
}

答案 4 :(得分:-2)

/**
 *
 * @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
    }

}