【发布时间】:2013-10-30 02:38:00
【问题描述】:
我正在做一个简单的程序来检查用户输入的单词是否是外来语言的一部分。 我得到一个索引超出范围的错误消息......有什么帮助吗? “字符串索引超出范围=”但是输入了许多字母。
import java.io.*;
import java.util.*;
import static java.lang.System.*;
public class AlienWords{
public static void main(String[]args) throws IOException {
Scanner scan = new Scanner(in);
out.print("Enter a word: ");
String str = scan.nextLine();
char d = str.charAt(str.length());
int florb = 0;
int zith = 0;
int wooble = 0;
int zarf = 0;
if (str.charAt(0) == str.charAt(str.length())){
florb++;
}
if (str.indexOf("cj") != -1 || str.indexOf("wq") != -1) {
wooble++;
}
if (str.length() % 2 == 1 && str.indexOf('z') == -1){
zith++;
}
if (str.length() % 2 == 0 && str.indexOf('k') == -1){
zith++;
}
if(Character.isUpperCase(d)){
zarf++;
}
else {
out.println("This is not an alien word.");
}
}
}
【问题讨论】:
-
以后,请发布确切的堆栈跟踪。它将包括行号。
标签: java string indexoutofboundsexception