【问题标题】:Java Error java.lang.StringIndexOutOfBoundsException: String index out of range: 0Java 错误 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0
【发布时间】:2016-11-15 11:51:40
【问题描述】:

我收到了这个异常以及这些:

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围: 0 在 java.lang.String.charAt(Unknown Source) 在 RandomWalk.matchingChoice(RandomWalk.java:50) 在 RandomWalk.main(RandomWalk.java:17) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)在 java.lang.reflect.Method.invoke(未知来源)在 edu.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,这将允许输入,但在输入后会出现相同的错误。任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: java exception


    【解决方案1】:

    你在 CONSOLE.nextLine(); 中犯了一个错误,像 CONSOLE.next(); 一样替换它 并且您的代码将正确运行。

    String colorChoice = CONSOLE.next();
    

    将所有CONSOLE.nextLine() 替换为CONSOLE.next()

    【讨论】:

    • nextLine() 和next() 的区别只是要插入一个新行。
    【解决方案2】:

    删除您的扫描仪声明中的最后一个:

    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();
    

    希望对你有帮助。

    【讨论】:

    • 如果我做对了,请检查我的答案。谢谢:)
    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    相关资源
    最近更新 更多