javaloopscalculatorquit

How do I fix my Java code so that it ends when a user types "quit"?


So I have been working on this simple calculator in Java for a while now, and I want the program to end when a user types "quit" in the scanner. I tried to implement this feature as an 'If' statement near the end of my main class in the following line:

if (input.equals("quit")) {
                System.out.println("Thanks for using my program.");
                System.exit(0);
            } 

My IDE doesn't recognize any errors, but when I compile and run my program, and attempt to exit the program by typing 'quit' into the scanner, the program ends (with errors) without displaying my exit message.

My code is as follows:

 package calculator;
    import java.util.Scanner;

    public class Calculator {

    public static void main(String[] args) {

        System.out.println("Hello, welcome to my calculator");
        System.out.println("Enter in some stuff you want to me to calculate");

        Scanner scan = new Scanner(System.in);
        System.out.println("If you need help please type \"help\"");
        System.out.println("If at anytime you want to leave, type \"quit\"");
        System.out.println("If you want to continue, hit enter.");

        String s1 = scan.nextLine();

        if (s1.equals("help")){
            System.out.println(" ");
            System.out.println("Double operand commands:");
            System.out.println("Addition: '+' (Ex: 'a + b' )");
            System.out.println("Subtraction: '-' (Ex: 'a - b' )");
            System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
            System.out.println("Division: '/' (Ex: 'a / b' )");
            System.out.println("Exponents: '^' (Ex: 'a ^ b' )");
            System.out.println(" ");
        }

        Scanner input = new Scanner(System.in);

        Maths maths = new Maths();

        double answer = 0;
        double numA, numB;
        char operator;
        boolean quit = false;

         while (quit == false) {
            System.out.print("Please enter your equation: ");

            numA = input.nextDouble();
            operator = input.next().charAt(0);
            numB = input.nextDouble();        

            if (operator == '+') {
                answer = maths.add(numA, numB);
            }

            if (operator == '-') {
                answer = maths.subtract(numA, numB);
            }

            if (operator == '*') {
                answer = maths.multiply(numA, numB);
            }

            if (operator == '/') {
                answer = maths.divide(numA, numB);
            }

            if (operator == '^') {
                answer = maths.power(numA, numB);
            }

            System.out.println(answer);        

        if (input.equals("quit")) {
            System.out.println("Thanks for using my program.");
            System.exit(0);
        }

         }       

        input.close();

        }

    }

    class Maths {

        double add(double a, double b) {
            double answer = a+b;
            return answer;          
        }

        double subtract(double a, double b) {
            double answer = a-b;
            return answer;          
        }

        double multiply(double a, double b) {
            double answer = a*b;
            return answer;          
        }

        double divide(double a, double b) {
            double answer = a/b;
            return answer;          
        }

        double power(double a, double b){
            double answer =a;

            for (int x=2; x<=b; x++){
                answer *= a;
            }

            return answer;
        }

    }

I would appreciate it if you could tell me what's wrong so that my code will finally work and so that I won't make a similar mistake in the future!

Thanks in advance


Solution

  • actually your boolean quit is redundant in this case.

    all you need to do is change loop to while (true){} and exit if user enter "quit". take a look this code.

    don't call nextDouble(); take first input as a String and check is it quit or not.if not then you can covert it to double using Double.parseInt() otherwise a number-format exception will be thrown and if user input is "quit" you can call system.exit(0); but also you can call return;

    package calculator;
    
     import java.util.Scanner;
    
        public class Calculator {
    
        public static void main(String[] args) {
    
            System.out.println("Hello, welcome to my calculator");
            System.out.println("Enter in some stuff you want to me to calculate");
    
            Scanner scan = new Scanner(System.in);
            System.out.println("If you need help please type \"help\"");
            System.out.println("If at anytime you want to leave, type \"quit\"");
            System.out.println("If you want to continue, hit enter.");
    
            String s1 = scan.nextLine();
    
            if (s1.equals("help")){
                System.out.println(" ");
                System.out.println("Double operand commands:");
                System.out.println("Addition: '+' (Ex: 'a + b' )");
                System.out.println("Subtraction: '-' (Ex: 'a - b' )");
                System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
                System.out.println("Division: '/' (Ex: 'a / b' )");
                System.out.println("Exponents: '^' (Ex: 'a ^ b' )");
                System.out.println(" ");
            }
    
            Scanner input = new Scanner(System.in);
    
            Maths maths = new Maths();
    
            double answer = 0;
            double numA, numB;
            char operator;
            boolean quit = false;
    
    
             while (true) {
                System.out.print("Please enter your equation: ");
                String s=input.next();
                if(s.equals("quit")){
                    System.out.println("Thanks for using my programe");
                    System.exit(0);
                }
                numA = Double.parseDouble(s);
                operator = input.next().charAt(0);
                numB = input.nextDouble();        
    
                if (operator == '+') {
                    answer = maths.add(numA, numB);
                }
    
                if (operator == '-') {
                    answer = maths.subtract(numA, numB);
                }
    
                if (operator == '*') {
                    answer = maths.multiply(numA, numB);
                }
    
                if (operator == '/') {
                    answer = maths.divide(numA, numB);
                }
    
                if (operator == '^') {
                    answer = maths.power(numA, numB);
                }
    
                System.out.println(answer);        
    
    
    
             }
    
            }
    
        }
    
        class Maths {
    
            double add(double a, double b) {
                double answer = a+b;
                return answer;          
            }
    
            double subtract(double a, double b) {
                double answer = a-b;
                return answer;          
            }
    
            double multiply(double a, double b) {
                double answer = a*b;
                return answer;          
            }
    
            double divide(double a, double b) {
                double answer = a/b;
                return answer;          
            }
    
            double power(double a, double b){
                double answer =a;
    
                for (int x=2; x<=b; x++){
                    answer *= a;
                }
    
                return answer;
            }
    }
    

    sample output >>

    Hello, welcome to my calculator
    Enter in some stuff you want to me to calculate
    If you need help please type "help"
    If at anytime you want to leave, type "quit"
    If you want to continue, hit enter.
    
    Please enter your equation: 2
    +
    3
    5.0
    Please enter your equation: quit
    Thanks for using my programe