有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如果没有选择正确的字母,如何打印错误

我有一个程序,它有一个列表,打印一个人员列表,并要求用户选择一个人

a--Rudolph Red
b--Dopey Dwarf
c--Sleepy Dwarf
d--Doc Dwarf
e--Happy Dwarf

如果选择了不同的字母而不是上面的字母,我试图打印一个错误。这些字母的长度可能会发生变化,所以如果是这样的话,我就做不到了!=a、 b、c、d、e我尝试使用IndexOutOfBoundsException,因为这是我在选择不同字母时遇到的错误

friendsListAdd();
add = sw.next(); 
char tv = add.charAt(0);
//where I am trying to do the error
if (tv != add.charAt(0))
{
    throw new IndexOutOfBoundsException("Letter does not exist");
}
friendsAdd(tv); 

 public void friendsListAdd()
{
int o = 0;
String Friends = "";
Friends +=  "Pick the letter of a friend to add from this list: ";
OneFace m  = face.get(0);

for(int w = 1; w <  profiles; w++)
    { 
    int g =  o + 97;
    char x = (char)g;
    String cake = face.get(w).getName();
    if (!m.checkFriends(cake))       
        {   
        // got help from Gokul with getting the names in ABC order.
        Friends += "\n\t" + x + "--" + cake;
        o++;
        combine.add(cake);
        }

    }
Friends += "\nPick letter:";
System.out.println(Friends);
}

public void friendsAdd(char hey)
{
int blue = (char)hey;
int nameS = blue - 97;
OneFace lon = face.get(0);
String addFriend ="";
addFriend = combine.get(nameS);
System.out.println(addFriend);
lon.addList(addFriend);
int newlon = checkProfile(addFriend);
OneFace l = face.get(newlon);
String ggg = face.get(0).getName();
System.out.println(ggg);
l.addList(ggg);
System.out.println(addFriend + " Has been added to "+ ggg 
+ "'s friend list");

}

共 (0) 个答案