我正在尝试创建一个菜单,通过数字系统方法将一组数字打印到屏幕上。当我尝试循环我的菜单时,我收到以下错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at homework4_5.Driver.engageDriver(Driver.java:31)
at homework4_5.Driver.engageDriver(Driver.java:47)
at homework4_5.Driver.main(Driver.java:18)
我也相信我的方法NumberSystem
不是问题。(与使用其他输入法一样,我可以获得产生正确结果的方法。)
我在文本菜单中经常遇到这个问题。谁能帮我?我认为可能是扫描仪正在接收一个空白字符串并且无法从中解析int
。
import java.util.Scanner;
public class Driver {
Scanner sc = new Scanner (System.in);
public int baseCount = 0;
public int numOfCols = 0;
public boolean loop = true;
public static void main(String[] args){
Driver driver = new Driver();
driver.engageDriver();
}
public void engageDriver(){
NumberSystem ns = new NumberSystem();
System.out.println("Hello, please enter the numeric value corresponding to your counting/base system" );
String temp = sc.nextLine();
if(temp.charAt(0)== 'q'){
System.out.println("Shutting Down the program...Thank You!");
loop = false;
return;
}
baseCount = Integer.parseInt(temp);
System.out.println("Now enter how many columns you would like to display ( a value from 1 to 3 ) ");
numOfCols=sc.nextInt();
System.out.println("The number table you requested is:\n ");
ns.printNumberSystem(baseCount, numOfCols);
if(loop){
engageDriver();
}
}
}