![](/img/trans.png)
[英]Java.Lang.Stringindexoutofboundsexception index out of range (0)
[英]Java Error java.lang.StringIndexOutOfBoundsException: String index out of range: 0
我正在与这些一起收到此异常:
java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:Sun处RandomWalk.main(RandomWalk.java:17)处RandomWalk.matchingChoice(RandomWalk.java:50)处java.lang.String.charAt(Unknown Source)处为0。 edu.sun.reflect上的reflect.NativeMethodAccessorImpl.invoke0(本地方法)edu.java.lang.reflect.Method.invoke(未知源)的sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)的NativeMethodAccessorImpl.invoke(未知源)。 rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)
运行此代码时:
import java.util.*;
import java.awt.*;
public class RandomWalk{
public static final Scanner CONSOLE = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Enter Circle Radius (50-400):");
int radius = CONSOLE.nextInt();
while (radius < 50 || radius > 400){
System.out.println("Radius Invalid; Try Again");
System.out.println("Enter Circle Radius (50-400):");
radius = CONSOLE.nextInt();
}
int diameter = radius*2;
int panelD = diameter/4;
System.out.println("Enter Color of Circle (Blue or Green)");
String colorChoice = CONSOLE.nextLine();
boolean test = matchingChoice(colorChoice, "blue");
boolean test2 = matchingChoice(colorChoice, "green");
while(test2 == false && test == false){
System.out.println("Invalid Entry Try Again");
System.out.println("Enter Color of Circle (Blue or Green)");
colorChoice = CONSOLE.nextLine();
test = matchingChoice(colorChoice, "blue");
test2 = matchingChoice(colorChoice, "green");
}
if(test == true){
Color circleColor = Color.BLUE;
} else {
Color circleColor = Color.GREEN;
}
System.out.println("Enter Color of Walk Path (Orange or Red)");
colorChoice = CONSOLE.nextLine();
test = matchingChoice(colorChoice, "orange");
test2 = matchingChoice(colorChoice, "red");
while(test2 == false && test == false){
System.out.println("Invalid Entry Try Again");
System.out.println("Enter Color of Circle (Orange or Red)");
colorChoice = CONSOLE.nextLine();
test = matchingChoice(colorChoice, "orange");
test2 = matchingChoice(colorChoice, "red");
}
if(test == true){
Color pathColor = Color.ORANGE;
} else {
Color pathColor = Color.RED;
}
}
public static boolean matchingChoice(String input, String choice){
input = input.toLowerCase();
char fLI = input.charAt(0);
char fLC = choice.charAt(0);
if(fLI == fLC){
return true;
} else if (input.equals(choice)){
return true;
} else {
return false;
}
}
}
错误就在之后
System.out.println("Enter Color of Circle (Blue or Green)");
我试图将nextLine更改为next,这将允许输入,但在输入后立即会出现相同的错误。 任何帮助将不胜感激,谢谢。
您在CONSOLE.nextLine();中犯了一个错误,将其替换为CONSOLE.next();。 并且您的代码将正确运行。
String colorChoice = CONSOLE.next();
将所有CONSOLE.nextLine()
替换为CONSOLE.next()
删除扫描仪声明中的最终文件:
public static final Scanner CONSOLE = new Scanner(System.in);
至
public static Scanner CONSOLE = new Scanner(System.in);
然后您将在CONSOLE
上实例化一个新的Scanner
,因为您将在CONSOLE Variable
上输入其他数据类型。
System.out.println("Enter Color of Circle (Blue or Green)");
CONSOLE = new Scanner(); // Instantiate new Scanner on your CONSOLE variable
colorChoice = CONSOLE.nextLine();
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.