我是Java的新手,我无法弄清楚如何解决这个问题: 线程" main"中的例外情况java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1
以下是整个代码:
ascii
非常感谢任何帮助。
答案 0 :(得分:1)
问题在于,char ch = input.next().charAt(0);
会读取两个字符,但不会"消费"用户按Enter键时生成的换行符。
然后调用input.nextLine()
,将使用换行符(但现在这两个字符已被next()
消耗,因此生成的字符串为空)。在空字符串上调用.charAt(1)
会生成异常,因为空字符串中没有位置(1)
(它的长度为0)。
相反,我建议你使用这样的东西:
//Step 2: Ask user to enter two characters
System.out.println("\nPlease enter two characters (1st character represents the major and 2nd character represents the classification): ");
String majorAndClassification = "";
while( majorAndClassification.length() != 2 )
majorAndClassification = input.nextLine();
char ch = majorAndClassification.charAt(0);
char ch1 = majorAndClassification.charAt(1);
这将确保用户输入两个字符 - 如果他没有,他将不得不再次尝试,直到他这样做。一个稍微好一点的选择当然是每次都打印出提示,如下:
//Step 2: Ask user to enter two characters
String majorAndClassification = "";
while( majorAndClassification.length() != 2 ){
System.out.println("\nPlease enter two characters (1st character represents the major and 2nd character represents the classification): ");
majorAndClassification = input.nextLine();
}
答案 1 :(得分:1)
如果输入第二个输入的字符,由于charAt(1)
,它会超出范围。只需将其更改为charAt(0)
,因为您总是只读取输入的第一个字符:
char ch = input.next().charAt(0);
char ch1 = input.next().charAt(0);
它适用于两个输入:
i
3
还有i 3
答案 2 :(得分:1)
致电
final char ch1 = input.nextLine().charAt(1);
期望读取整行,并获得第二个字母。 但
char ch = input.next().charAt(0);
已经消耗了第一个字符。
所以你应该一次性阅读整个输入,然后(做更多检查,然后)获取你的字符。
//Step 2: Ask user to enter two characters
System.out.println("\nPlease enter two characters (1st character represents the major and 2nd character represents the classification): ");
final String reply = input.nextLine();
final char ch = reply.charAt(0);
final char ch1 = reply.charAt(1);
//Step 3: Print statement
答案 3 :(得分:1)
尝试更改代码,使其在引用String的索引之前首先检查输入的长度。如下所示:
//Step 2: Ask user to enter two characters
System.out.println("\nPlease enter two characters (1st character represents the major and 2nd character represents the classification): ");
String inputNext = input.next();
String inputNextLine;
if(inputNext.length>0 && inputNextLine.length>0){
char ch = inputNext;
char ch1 = inputNextLine;
//Step 3: Print statement
switch(ch)
{
case 'i': major = "Information Technology";
break;
case 'c': major = "Computer Science";
break;
case 'm': major = "Mathematics";
break;
case 'p': major = "Physics";
break;
case 'b': major = "Biology";
break;
case 'e': major = "Engineering";
break;
case 'h': major = "History";
break;
case 'j': major = "Journalism";
break;
case 'a': major = "Art and Design";
break;
case 'l': major = "Literature";
break;
case 's': major = "Sport Medicine";
break;
default: System.out.println("\nInvalid Major Code");
System.out.println("Please enter a character followed by an integer!");
break;
}//end of switch
//Step 3: Print statement
switch(ch1)
{
case '1': classification = "Freshman";
break;
case '2': classification = "Sophmore";
break;
case '3': classification = "Junior";
break;
case '4': classification = "Senior";
break;
case '5': classification = "Graduate";
break;
}
}
答案 4 :(得分:1)
从char ch1 = input.nextLine().charAt(1);
更改您的代码
这个char ch1 = input.next().charAt(0);
您可以查看this,了解有关next()和nextLine()之间差异的详细信息