Verify that the string contains other types of characters

1

I'm developing a project that works with logical expressions, for example it receives as input:

((TvF->T)^(T->F))->(~F<->T)

I then do all the validation to remove whitespace, replace -> with - and <-> with - .

How can I make a validation in the expression string to check if the user typed some other character that is not one of these:

(,T,v,F,->,<->,~,),^?
    
asked by anonymous 07.03.2018 / 14:18

1 answer

0

Just to validate if the caracters are valid can do something like this:

    String operation = "((TvF->T)^(T->F))->(~F<->T)";
    Set<String> validStrings = new HashSet<String>(
        Arrays.asList(new String[]{ "(", "T", "v", "F", "->", "<->", "~", ")", "^", "?" })
        );
    Set<Character> combinedChars = new HashSet<Character>(
        Arrays.asList(new Character[]{ '-', '<' })
        );

    boolean isValidOp = true;
    StringBuilder subString = new StringBuilder();
    for (int i = 0; i < operation.length(); i++) {
        subString.append(operation.charAt(i));
        if (validStrings.contains(subString.toString())) {
            subString = new StringBuilder();
        } else {
            if (!combinedChars.contains(operation.charAt(i))) {
                isValidOp = false;
                break;
            }
        }
    }

    System.out.println(isValidOp);

But for what logical expressions validation will need something else, as mentioned in the comments would have to ensure that for example the parentheses are closed when opened.

    
11.03.2018 / 14:01