我有以下问题:我想从用户那里读取一个字符串,到目前为止它工作得很好但是每次我按下“return”我总是得到以下错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at Shell.execute(Shell.java:20)
at Shell.main(Shell.java:55)
这将是代码:
private static void execute(BufferedReader stdin) throws IOException {
boolean quit = false;
Field test = new Field();
while (!quit) {
System.out.print("ch> ");
String input = stdin.readLine();
if (input == null) {
break;
}
String[] tokens = input.trim().split("\\s+");
tokens[0].toLowerCase();
char tmp = tokens[0].charAt(0);
switch (tmp) {
case 'n':
test.setPoints(null);
break;
case 'a':
test.add(new Point(Integer.parseInt(tokens[1]), Integer
.parseInt(tokens[2])));
break;
case 'r':
test.remove(new Point(Integer.parseInt(tokens[1]), Integer
.parseInt(tokens[2])));
break;
case 'p':
System.out.println(test);
break;
case 'c':
System.out.println(test.convexHull());
break;
case 'h':
System.out.println("");
break;
case 'q':
quit = true;
break;
default:
break;
}
}
}
谢谢你的帮助。