gpt4 book ai didi

Java:如何处理字符串索引超出范围错误?

转载 作者:行者123 更新时间:2023-12-01 16:53:04 26 4
gpt4 key购买 nike

我是 Java 新手,不知道如何解决这个问题:线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1

完整代码如下:

public static void main(String[] args) {

// Create a Scanner
Scanner input = new Scanner(System.in);

//Step 1: Ask user to enter first and last name
System.out.println("\nPlease enter your first and last name: ");
String name = input.nextLine();
String major = "";
String classification = "";

//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): ");
char ch = input.next().charAt(0);
char ch1 = input.nextLine().charAt(1);

//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;
default: System.out.println("\nInvalid Classification Code");
System.out.println("Please enter a character followed by an integer!");
break;
}//end of switch

System.out.println("\nMajor and classification is: " + major + "" + classification);
System.out.println("\nThank You!");
}//end of main

任何帮助将不胜感激。

最佳答案

尝试更改您的代码,以便它首先检查输入的长度,然后再引用字符串的索引。类似于以下内容:

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

关于Java:如何处理字符串索引超出范围错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36384357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com