检查字符串是否包含无效字符

时间:2018-01-24 21:35:36

标签: java string char

我正在尝试编写一个以字符串作为输入的程序。

此字符串必须是仅包含括号,操作或数字的等式。

我在下面发布的代码是我提出的,但是当我运行它时,我输入的任何内容显然都是无效的字符串。有人可以告诉我做错了吗?

        System.out.println("Enter a string");                               
        String str = s.nextLine();   //s is a Scanner object                    
        int n = str.length();                                           

        for(int i = 0; i < n; i++) {                                        
            if( !Character.isDigit(str.charAt(i)) || str.charAt(i) != '+'
                || str.charAt(i) != '-' || str.charAt(i) != '*'
                || str.charAt(i) != '/' || str.charAt(i) != '('
                || str.charAt(i) != ')' || str.charAt(i) != '['
                || str.charAt(i) != ']' || str.charAt(i) != '{'
                || str.charAt(i) != '}') {
                    System.out.println("invalid string, try again: ");
                    str = s.nextLine();     
            }
        ...}

3 个答案:

答案 0 :(得分:3)

根据评论中的建议,为||切换&&

if( !Character.isDigit(str.charAt(i)) && str.charAt(i) != '+'
        && str.charAt(i) != '-' && str.charAt(i) != '*'
        && str.charAt(i) != '/' && str.charAt(i) != '('
        && str.charAt(i) != ')' && str.charAt(i) != '['
        && str.charAt(i) != ']' && str.charAt(i) != '{'
        && str.charAt(i) != '}') {
    System.out.println("invalid string, try again: ");
    str = s.nextLine();     
}

或者,为了让您的代码更容易理解:

final String validChars = "0123456789+-*/()[]{}";
for(int i = 0; i < n; i++) {
    if(!validChars.contains(str.charAt(i))) {
        System.out.println("invalid string, try again: ");
        str = s.nextLine();
        // potential bug here - i and n are not being reset
    }
}

注意:您有一个错误,在您从扫描仪中读取新行时,您不会重置i索引或n长度前一行包含无效字符的位置(请参阅上面代码中的注释)。

答案 1 :(得分:1)

如果您不想更改所有代码,可以:

 System.out.println("Enter a string");

    String str = s.nextLine(); // s is a Scanner object
    int n = str.length();

    if (n > 0) {
        for (int i = 0; i < n; i++) {
            if (!(Character.isDigit(str.charAt(i)) || str.charAt(i) == '+' || str.charAt(i) == '-'
                    || str.charAt(i) == '*' || str.charAt(i) == '/' || str.charAt(i) == '(' || str.charAt(i) == ')'
                    || str.charAt(i) == '[' || str.charAt(i) == ']' || str.charAt(i) == '{'
                    || str.charAt(i) == '}')) {
                System.out.println("invalid string, try again: ");
                str = s.nextLine();
                n = str.length();
                i = -1;//reset i for the new line ( set at -1 for the next i++ of the next loop )
            }
        }
    } else {
        System.out.println("empty string ");
    }

答案 2 :(得分:0)

package hello;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class Characts {

    @Test
    public void testSwitch () {
         assertTrue(isValid("123+321*"));
         assertFalse(isValid("A123+321*"));
         assertTrue(isValid("123+321*\n")); // don't forget returns and line feeds
    }

    private boolean isValid(String str) {
        for (int i = 0 ; i < str.length(); i++) {
            char s = str.charAt(i);
            if (Character.isDigit(s)) {
                continue;
            } else {
                switch (s) {
                case '+' :
                case '-' :
                case '*' :
                case '/' :
                case '(' :
                case ')' :
                case '[' :
                case ']' :
                case '{' :
                case '}' :
                    continue;
                }
                return false;
            }
        }
        return true;
    }
}
相关问题