dimanche 24 septembre 2017

RPN Calculator using Scanner, If/Else statement not reading

I am working on a very simple reverse polish notation calculator using stacks, and having trouble with an if/else statement inside a while loop. The rest of the code is working correctly, but I keep getting a NumberFormatException after the else line. I think this means it is not doing what it says to do in the if statement when the input is "*", but I am not sure how to fix this. Why would it go to else if the input is equal to one of the specified characters? I am not interested in making code more efficient, I would like to keep the format I have if possible, but I am very confused. Thank you so much for your help.

  try {            
        Scanner stackScanner = new Scanner(stackFile);
        while (stackScanner.hasNextLine()) {
            String pls = "+";
            String min = "-";
            String mul = "*";
            String div = "/";
            String mod = "%";
            String exp = "^"; 
            //stackScanner.nextLine();
            if(stackScanner.nextLine().equals(pls) ||
                stackScanner.nextLine().equals(min) ||
                stackScanner.nextLine().equals(mul) ||
                stackScanner.nextLine().equals(div) ||
                stackScanner.nextLine().equals(mod) ||
                stackScanner.nextLine().equals(exp)) {
                stack.push(new operationNode(stackScanner.nextLine()));
            }
            else {
                stack.push(new 
                 numberNode(Double.parseDouble(stackScanner.nextLine())));  
            }

        }

        stackScanner.close();    

Aucun commentaire:

Enregistrer un commentaire