s.hasNext(pattern) falsely returning true

Viewed 26

I have a program that checks for a vector expression and executes other functions if it detects different operators. For example, < 1, 3 > + < 1, 4 > will call the add function. My issue is that when I check my input for operators, some inputs will call functions even when the pattern is not there. I am using hasNext(pattern "+") to do this, to check for +, -, or . in my input. Some inputs, like "<" or just the enter button will cause the if statement to be executed.

System.out.println("Enter expression: ");
        if (s.hasNext("\\+") || s.hasNext("-") || s.hasNext(".")){
            parseVectorExpression(s);
        else{
            throw new Exception ("No expression detected");
            }
        }

It will start parseVectorExpression even if +, -, or . has not been entered. Please help.

0 Answers
Related