简体   繁体   中英

Multiple || statements in one if statement

I'm trying to have my program check the name that the player enters to see if it uses any invalid characters. It seems to make sense in my mind how to make it work, but it seems not to work in the actual environment. When I run it, and I use invalid characters then it will just go on to the next part of the program. Any Ideas on how to fix it?

Here is my relevant code block:

if (!(nameInput.getText().equals(""))) {
    boolean allow = true;
    String check = nameInput.getText();
    for (int i = 0;i>check.length();i++) {
        if (check.charAt(i) == '`' || check.charAt(i) == '~' ||
                check.charAt(i) == '!' || check.charAt(i) == '@' ||
                check.charAt(i) == '#' || check.charAt(i) == '$' ||
                check.charAt(i) == '%' || check.charAt(i) == '^' ||
                check.charAt(i) == '&' || check.charAt(i) == '*' ||
                check.charAt(i) == '(' || check.charAt(i) == ')' ||
                check.charAt(i) == '_' || check.charAt(i) == '+' ||
                check.charAt(i) == '=' || check.charAt(i) == '[' ||
                check.charAt(i) == ']' || check.charAt(i) == '{' ||
                check.charAt(i) == '}' || check.charAt(i) == ';' ||
                check.charAt(i) == '\''|| check.charAt(i) == ':' ||
                check.charAt(i) == '"' || check.charAt(i) == '<' ||
                check.charAt(i) == '>' || check.charAt(i) == '?' ||
                check.charAt(i) == ',' || check.charAt(i) == '.' ||
                check.charAt(i) == '/' || check.charAt(i) == '\\'||
                check.charAt(i) == '|' || check.charAt(i) == '1' ||
                check.charAt(i) == '2' || check.charAt(i) == '3' ||
                check.charAt(i) == '4' || check.charAt(i) == '5' ||
                check.charAt(i) == '6' || check.charAt(i) == '7' ||
                check.charAt(i) == '8' || check.charAt(i) == '9' ||
                check.charAt(i) == '0') {
            allow = false;
        }
    }
    if (allow == true) {
        player = new Player(check);
        window.refresh();
        mainMenu();
    } else {
        JOptionPane.showMessageDialog(window, "Invalid Name(Uses invalid Characters","Invalid!", JOptionPane.OK_OPTION);
    }
} else {
    JOptionPane.showMessageDialog(window, "Can Not Leave Name Blank!", "Missing Name!", JOptionPane.OK_OPTION);
    window.refresh();
    createPlayer();
}
}
for(int i = 0;i>check.length();i++)

Your for loop is never going to be entered, as I don't believe a String 's length can ever be negative...

Did you mean to use < instead?

Also, there are significantly better ways to check for valid names than a gigantic chain of character checks.

One way is by using regular expressions; check out Java's Pattern class and Google to learn regex. That way you can reduce your loop-and-if-block to this (I think; I just accepted alphabetic characters only and rejected everything else based on a really quick glance at that monstrosity):

allow = String.matches("[a-zA-Z]{1,}");

If you really are interested in allowing only Strings that consist of characters not on that list, you can do it with a regular expression like this.

allow = check.matches("[^]`~!@#$%^&*()_+=[{};':\"<>?,./\\\\|1234567890]*");

This will set allow to true if check is made up entirely of characters not on the list.

Note that I placed ^ just inside the [ - this means "not". I also moved ] to immediately after the ^ , so that it's not interepreted as closing the list.

Change

for(int i = 0;i>check.length();i++)

to

for(int i = 0;i<check.length();i++)

Since i is 0 which is obviously less that check.length() program flow never really enters the for loop.

Also instead of just check for "" add a null check.

if(nameInput.getText()!=null && !(nameInput.getText().equals(""))){..}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM