2016-11-15 115 views
0

我收到此異常與這些一起:Java錯誤java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:0

java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:在java的 0 .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(未知來源)在 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到明年這將允許輸入,但輸入以後會出現同樣的錯誤。任何幫助將得到很多讚賞,謝謝。

回答

0

您在CONSOLE.nextLine()中犯了一個錯誤;將其替換爲CONSOLE.next(); 和您的代碼將正確運行。

String colorChoice = CONSOLE.next(); 

更換所有CONSOLE.nextLine()CONSOLE.next()

+0

nextLine()和next()之間的區別只是要插入的新行。 – msagala25

0

刪除最後你對自己掃描器聲明:

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

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

,然後你會實例化一個新的Scanner你的CONSOLE是因爲您將在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(); 

希望它有幫助。

+0

這就是問題所在,非常感謝! –

+0

如果我把它正確的請把我的答案檢查。謝謝 :) – msagala25

相關問題