我正在尝试打印姓氏字符,但我的代码生成了异常。
代码:
import java.util.Scanner;
public class LastCharacter {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type your name: ");
String name = reader.nextLine();
char nameLastChar = lastCharacter(name);
System.out.println("Last character = " + nameLastChar);
}
public static char lastCharacter(String text){
int last = text.length();
char lastChar = text.charAt(last);
return lastChar;
}
}
例外:
线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:7
我找不到我的错误,我不明白异常信息。
largeQ
相关分类