Home » Java » java – How to update output with continuous user inputs until terminated with "."?-Exceptionshub

java – How to update output with continuous user inputs until terminated with "."?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

New to Java and trying to figure out how to keep a running evaluated expression given by a user continuously until they type in “.” to terminate it and then print the output. I need to use methods outside of the main that I have already placed.I don’t have to worry about order of operation but I’m confused on how to take on a continuous amount of operations/numbers while keeping track of their outputs and then how to finally print the result when they put “.”.

import java.util.Scanner;

public class Evaluator {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int num1;
        char expression;
        int num2;
        String numlist = "";

        System.out.println("Enter the expression: ");
        num1 = scan.nextInt();
        expression = scan.next().charAt(0);
        num2 = scan.nextInt();
        scan.close();

        if (expression == '+') {
            add(num1, num2);
        } else if (expression == '-') {
            subtract(num1, num2);
        } else if (expression == '*') {
            multiply(num1, num2);
        } else if (expression == '/') {
            divide(num1, num2);
        } else if (expression == '%') {
            modulus(num1, num2);
        } else if (expression == '.'){

        }
    }

    static int add(int num1, int num2) {
        return (num1 + num2);
    }

    static int subtract(int num1, int num2) {
        return (num1 - num2);
    }

    static int divide(int num1, int num2) {
        return (num1/num2);
    }

    static int multiply(int num1, int num2) {
        return (num1*num2);
    }

    static int modulus(int num1, int num2) {
        return (num1%num2);
    }
}

OUTPUT:

Enter the expression:

1
+
26
*
2
%
19
/
5
.

The result is 3

How to&Answers:

I would recommend to start with “0 +” followed by whatever the user entered. Because then you do not need special handling for the first number.

import java.util.Scanner;

class Main
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the expression: ");

        // Start with "0 +"
        int value = 0;
        String operator = "+";

        while (true) // loop
        {
            int operand = scan.nextInt();

            switch (operator)
            {
                case "+": value += operand; break;
                case "-": value -= operand; break;
                case "*": value *= operand; break;
                case "/": value /= operand; break;
                case "%": value %= operand; break;
                default: throw new UnsupportedOperationException(operator);
            }

            operator = scan.next();

            if (".".equals(operator))
            {
                break; // exit the loop
            }
        }

        System.out.println("result=" + value);
        scan.close();
    }

}

However, this algorithm does not care mathematical rules of operator order. it simply processes the input from left to right like a cheap 3€ pocket calculator.

Other example based on your if-else construct and methods:

import java.util.Scanner;

class Main
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the expression: ");

        // Start with "0 +"
        int value = 0;
        char operator = '+';

        while (true) // loop
        {
            int operand = scan.nextInt();

            if (operator == '+') {
                value=add(value, operand);
            } else if (operator == '-') {
                value=subtract(value, operand);
            } else if (operator == '*') {
                value=multiply(value, operand);
            } else if (operator == '/') {
                value=divide(value, operand);
            } else if (operator == '%') {
                value=modulus(value, operand);
            } else {
                throw new UnsupportedOperationException(String.valueOf(operator));
            }

            operator = scan.next().charAt(0);

            if (operator=='.')
            {
                break; // exit the loop
            }
        }

        System.out.println("result=" + value);
        scan.close();
    }

    static int add(int num1, int num2) {
        return (num1 + num2);
    }

    static int subtract(int num1, int num2) {
        return (num1 - num2);
    }

    static int divide(int num1, int num2) {
        return (num1/num2);
    }

    static int multiply(int num1, int num2) {
        return (num1*num2);
    }

    static int modulus(int num1, int num2) {
        return (num1%num2);
    }
}

Answer:

Something like:

while(expression!='.'){
 if (expression == '+') {
        add(num1, num2);
    } else if (expression == '-') {
        subtract(num1, num2);
    } else if (expression == '*') {
        multiply(num1, num2);
    }else if (expression == '/') {
        divide(num1, num2);
    } else if (expression == '%') {
        modulus(num1, num2);
    }
}

Is this what you’re asking ?