在 Java 中,如果没有传递参数,如何为变量 number=Integer.parseInt(args[0]) 赋值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24842666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Java, how to assign the variable number=Integer.parseInt(args[0]) a value if no argument is passed?
提问by CordlessBrain
I wrote this simple program, that takes a command line argument, converts it into int type, and prints integers starting from that number till infinity on console.
我编写了这个简单的程序,它接受一个命令行参数,将其转换为 int 类型,并在控制台上打印从该数字开始到无穷大的整数。
If i don't pass any argument, then my exception message is printed.
如果我不传递任何参数,则会打印我的异常消息。
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.");
}
}
}
Is there any way that if i don't pass any argument, the program itself assigns a value to 'n'? Like this:
有没有办法,如果我不传递任何参数,程序本身会为“n”分配一个值?像这样:
n=Integer.parseInt(args[0]);
if(args[0]==NULL)
{
n=0;
}
采纳答案by Mauno V?h?
You could assign value of n
as 0
or any other value by default and use if(args.length > 0) {
to check whether any arguments is given. Below is full example with comments:
默认情况下,您可以分配n
as0
或任何其他值,并用于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++;
}
}
}
Note:Users which are not so familiar with java, this program can be run by:
注意:对java不太熟悉的用户,可以通过以下方式运行该程序:
- Saving it to
Infinity.java
- Compiling it with cmd or terminal by writing:
javac Infinity.java
- Executing it with:
java Infinity
orjava Infinity 1000
(or any other value)
- 保存到
Infinity.java
- 用 cmd 或终端编译它:
javac Infinity.java
- 使用:
java Infinity
或java Infinity 1000
(或任何其他值)执行它
Cheers.
干杯。
回答by Brett Okken
Check the length of the args
array before attempting to access.
args
在尝试访问之前检查数组的长度。
int n = 0;
if (args.length == 1)
{
n = Integer.parseInt(args[0]);
}
You still likely need a try/catch block if you want to handle an argument being provided that is not a valid integer.
如果您想处理提供的不是有效整数的参数,您仍然可能需要一个 try/catch 块。
回答by bcsb1001
Check if you have the arg, then parse it if you do. Otherwise, use your default value.
检查您是否有 arg,如果有则解析它。否则,请使用您的默认值。
if (args.length == 0) {
n = // default value
}
else {
n = Integer.parseInt(args[0]);
}
回答by Salathiel Genèse
/**
*
* @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
}
}
回答by Nivedita
Yes there is! You can check the length of args[]
so that when its 0 i.e no arguments were passed, a default value is assigned. Here's the simplest code:
就在这里!您可以检查 的长度,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++;
}
}
}
}