简体   繁体   中英

Java Error java.lang.StringIndexOutOfBoundsException: String index out of range: 0

I'm receiving this exception along with these:

java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at RandomWalk.matchingChoice(RandomWalk.java:50) at RandomWalk.main(RandomWalk.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

when running this code:

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;
    }
  }
}

the error comes just after

System.out.println("Enter Color of Circle (Blue or Green)");

I've attempted changing the nextLine to next and that would allow input but right after input the same error would appear. Any Help would be Much Appreciated, thank you.

you have make a mistake in CONSOLE.nextLine();, replace it like CONSOLE.next(); and your code will run correctly.

String colorChoice = CONSOLE.next();

replace all CONSOLE.nextLine() to CONSOLE.next()

Remove the final on your declaration of your Scanner:

public static final Scanner CONSOLE = new Scanner(System.in);

to

public static Scanner CONSOLE = new Scanner(System.in);

and then you will instantiate a new Scanner on your CONSOLE because you will input a different data type on your 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();

hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM