Ad

Java Calculator Program Issue

I am trying to develop a calculator program that inputs an arithmetic expression of the form "number operator number =" and computes the result of the expression. The expression will be evaluated from left to right not considering regular operator precedence. For example, the expression "14 - 5 * 3 =" will produce 27.0. The value "=" displays the final result and terminates the program.

I've been trying to fix this for a couple of days now, but it outputs the wrong answer whenever I enter an expression with more than two numbers. For instance, 2.8 + 2 - 9.5 should equal -4.7 but the program outputs -6.7. Any idea why that is the case?

import java.util.Scanner;

public class Calculator {

    // Compute an arithmetic expression 
    public static void main(String[] args) {
        // Declare the identifiers
        final String END = "=";
        String input;
        double num1 = 0;
        double num2 = 0;
        char operator = 0;
        
        Scanner scnr = new Scanner (System.in);
        
        System.out.println("Enter your numeric expression in the following form: ");
        System.out.println("number operator number operator number = ");
        System.out.println("Leave a blank space after each number or operator.");
        System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");

        // Input the first item
        System.out.print("> ");
        input = scnr.next();
        
        // Process the first item and input and process the rest of the items 
        while (!input.equals(END)){
            switch (input){
                case "+":               
                    operator = '+';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "-":               
                    operator = '-';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "*": 
                    operator = '*';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "/": 
                    operator = '/';
                    System.out.println("> Operator is: " + operator);
                    break;                  
                default: // a number was entered
                    if (num1 == 0) {
                        num1 = Double.parseDouble(input);
                        System.out.println("> Num1 is: " + num1);
                    }
                    else {
                        num2 = Double.parseDouble(input);
                        System.out.println("> Num2 is: " + num2);
                    }
                    
            } // end of switch
            
            if (num1 != 0 && num2 != 0) {
                
                System.out.println("Num2 before calc is " + num2);
                
                switch (operator) {
                    case '+':               
                        num2 = num1 + num2;
                        break;
                    case '-':               
                        num2 = num1 - num2;
                        break;
                    case '*': 
                        num2 = num1 * num2;
                        break;
                    case '/': 
                        num2 = num1 / num2;
                        break;
                    default:
                }
            }
            input = scnr.next();
            
        } // end of while-loop
        
        // Display the answer
        System.out.println("> Answer is: " + num2);
        
        
        
        System.out.println("Have a nice day!");
        
    }
}

Ad

Answer

In order to make it work, try to:

  • in your 2nd switch statement, change num2 = num1 + num2; into num1 = num1 + num2;. Do this for all cases;
  • I added an isOperator boolean to skip computing the operation if input is an operator.

Full code below:

import java.util.Scanner;

public class Calculator {

    // Compute an arithmetic expression 
    public static void main(String[] args) {
        // Declare the identifiers
        final String END = "=";
        String input;
        double num1 = 0;
        double num2 = 0;
        char operator = 0;
        boolean isOperator;
        
        Scanner scnr = new Scanner (System.in);
        
        System.out.println("Enter your numeric expression in the following form: ");
        System.out.println("number operator number operator number = ");
        System.out.println("Leave a blank space after each number or operator.");
        System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");

        // Input the first item
        System.out.print("> ");
        input = scnr.next();
        
        // Process the first item and input and process the rest of the items 
        while (!input.equals(END)){
            isOperator = true;
            switch (input){
                case "+":               
                    operator = '+';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "-":               
                    operator = '-';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "*": 
                    operator = '*';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "/": 
                    operator = '/';
                    System.out.println("> Operator is: " + operator);
                    break;                  
                default: // a number was entered
                    isOperator = false;
                    if (num1 == 0) {
                        num1 = Double.parseDouble(input);
                        System.out.println("> Num1 is: " + num1);
                    }
                    else {
                        num2 = Double.parseDouble(input);
                        System.out.println("> Num2 is: " + num2);
                    }
                    
            } // end of switch
            
            // do not compute the operation if the input is an operator and num1,num2 != 0
            if (num1 != 0 && num2 != 0 && !isOperator) {
                
                System.out.println("Num2 before calc is " + num2);
                
                switch (operator) {
                    case '+':               
                        num1 = num1 + num2;
                        break;
                    case '-':               
                        num1 = num1 - num2;
                        break;
                    case '*': 
                        num1 = num1 * num2;
                        break;
                    case '/': 
                        num1 = num1 / num2;
                        break;
                    default:
                }
            }
            input = scnr.next();
            
        } // end of while-loop
        
        // Display the answer
        System.out.println("> Answer is: " + num1);
        
        
        
        System.out.println("Have a nice day!");
        
    }
}
Ad
source: stackoverflow.com
Ad