英文:
Error in code: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
问题
double t = Double.parseDouble(args[0]);
int r = Integer.parseInt(args[0]);
System.out.println("r = " + r + ", t = " + t);
这部分代码无法编译。我找不到问题出在哪里。
错误代码:
异常信息:在线程 "main" 中出现 java.lang.ArrayIndexOutOfBoundsException: 0
英文:
double t = Double.parseDouble(args[0]);
int r = Integer.parseInt(args[0]);
System.out.println("r = " + r + ", t = " + t);
This part of the code is not compiling. I couldn't find what's wrong.
Error Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
答案1
得分: 0
在Java中,args
包含作为String对象数组的提供的命令行参数。
换句话说,如果你将程序运行为java DemoMain one two
,那么args将包含["one", "two"]
。
在你的情况下,你没有传递任何args,这导致了Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
。
你没有传递args的值。尝试通过命令行传递值给你。
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
public class DemoMain {
public static void main(String[] args) {
System.out.println(args[0]);
double t;
t = Double.parseDouble(args[0]);
double r;
r = Double.parseDouble(args[0]);
}
}
英文:
In Java, args
contains the supplied command-line arguments as an array of String objects.
In other words, if you run your program as java DemoMain one two
then args will contain ["one", "two"]
.
In your case you are not passing any args which result in Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
You are not passing the value of args. Try to pass the value through the command line you.
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
public class DemoMain {
public static void main(String[] args) {
System.out.println(args[0]);
double t;
t = Double.parseDouble(args[0]);
double r;
r = Double.parseDouble(args[0]);
}
}
答案2
得分: 0
以下为您翻译的内容:
在您的帖子标题中指出的错误与您在实际帖子中指出的错误相矛盾。标题中的错误表明您正在接受多个参数,而第一个参数成功了,但没有第二个参数。帖子正文中指出的错误表明根本没有提供任何参数。究竟是哪一种情况呢?
根据您帖子中指出的错误,
异常线程 "main" java.lang.ArrayIndexOutOfBoundsException: 0
没有检测到任何参数。换句话说,args[] 字符串数组为空(0 个元素)...在从终端命令提示符启动时未提供任何参数。
为了保护您的应用程序免受这种情况的影响,您可以这样做:
public static void main(String args[]) {
// 是否提供了参数?
if (args.length == 0) {
// 没有...
System.out.println("失败!- 未提供参数!");
return;
}
/* 提供的参数是带符号或无符号整数、双精度或单精度数值的字符串表示吗? */
else if (!args[0].matches("-?\\d+(\\.\\d+)?")) {
// 不是...
System.out.println("失败!- 提供的数值参数无效!");
return;
}
double t = 0.0d;
int r = 0;
// 提供了双精度或单精度数值吗?
if (args[0].contains(".")) {
// 是的 ... 那么我们将其转换为双精度。
t = Double.parseDouble(args[0]);
}
else {
// 不是 ... 那么我们将其转换为整数(int)。
r = Integer.parseInt(args[0]);
}
// 在控制台窗口中显示结果。
System.out.println("r = " + r + ", t = " + t);
}
您还需要注意,不能将浮点数的字符串表示形式传递给 Integer.parseInt() 方法。这是因为句点(.)被视为字母字符(类似于空格或字母 A),Integer.parseInt() 方法不会接受它。如果尝试这样做,将引发 NumberFormatException,应用程序会失败停止。然而,Integer.parseInt() 方法将接受以 -
或 +
字符为前缀的值(例如:Integer.parseInt("+50"))。我们在上面的代码示例中也考虑到了这一点(请阅读代码中的注释)。
英文:
The error indicated within your Post Title is in contradiction with the error you indicated within your actual post. The Error in the title indicates you are accepting more than one argument whereas the first argument was successful but there was no second argument. The error indicated within your post body indicates that there was no argument supplied at all. Which is it?
Based on the error indicated within your post,
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
there are no arguments detected. In other words the args[] String Array contains nothing (0 elements)...no arguments were supplied at startup from the Terminal Command Prompt.
To save-guard your application from this situation you could do something like this:
public static void main(String args[]) {
// Were arguments supplied?
if (args.length == 0) {
// No...
System.out.println("Failure! - No Argument Supplied!");
return;
}
/* Is the argument supplied a string representation of a signed
or unsigned Integer or double/float numerical value? */
else if (!args[0].matches("-?\\d+(\\.\\d+)?")) {
// No...
System.out.println("Failure! - Invalid Numerical Argument Supplied!");
return;
}
double t = 0.0d;
int r = 0;
// Has a double or float value been supplied?
if (args[0].contains(".")) {
// Yup ... So let's convert to double.
t = Double.parseDouble(args[0]);
}
else {
// Nope ... So let's convert to Integer (int).
r = Integer.parseInt(args[0]);
}
// Display the result in Console Window.
System.out.println("r = " + r + ", t = " + t);
}
You will also need to take note that you can not pass a string representation of a floating point value to the Integer.parseInt() method. This is because the period (.) is considered an alpha character (like a whitespace or the letter A) which the Integer.parseInt() method will not accept. If you try a NumberFormatException is thrown and your application halts in failure. The Integer.parseInt() method will however accept a value prefixed with a -
or +
character (ie: Integer.parseInt("+50"). We've also taken this into consideration in the above code example (read the comments in code).
专注分享java语言的经验与见解,让所有开发者获益!
评论