0

I am making a text based calculator that can do addition, subtraction, division, multiplication and powers. The code I have written so far seems to work, but when it reaches the section where it determines if the inputed text is a +, -, /, *, or a ^, it endlessly loops and doesn't recognize the input.

    double inputX;  // Used for input 2
    double inputY;  // Used for input 1
    double output;  // Used for math purposes

    String inputS;  // Used for string input

    String subt;   // Store the - in a string
    subt = "-";

    boolean corrInput = true;
    boolean repeat = true;

    while (repeat == true){

        System.out.print("Please enter a number: ");

        inputX = TextIO.getlnDouble();  // Retrieves inputX

        System.out.println("x: " + inputX + "\n");

        System.out.print("Please enter a number: ");

        inputY = TextIO.getlnDouble();  // Retrieves inputY

        System.out.println("y: " + inputY + "\n");


        while (corrInput == true){
            System.out.println("Choose an operation: +, -, *, /, ^.");
            inputS = TextIO.getlnString();  // Retrieves a strings input


            // Calculator logic

            if (inputS == subt){
                output = inputX - inputY;   // X minus Y

                System.out.println("Calculatorator says that " + inputX + " - " + inputY
                    + " = " + output);
            }

            else if (inputS == "+"){
                output = inputX - inputY;   // X plus Y

                System.out.println("Calculatorator says that " + inputX + " + " + inputY
                    + " = " + output);
            }

            else if (inputS == "*"){
                output = inputX * inputY;   // X multiplied by Y

                System.out.println("Calculatorator says that " + inputX + " * " + inputY
                    + " = " + output);
            }

            else if (inputS == "/"){
                output = inputX / inputY;   // X divided by Y

                System.out.println("Calculatorator says that " + inputX + " / " + inputY
                    + " = " + output);
            }

            else if (inputS == "^"){
                output = Math.pow(inputX, inputY);  // X to the power of Y

                System.out.println("Calculatorator says that " + inputX + " ^ " + inputY
                    + " = " + output);
            }


            else { 
                System.out.println("Please provide a valid input. '" + inputS
                    + "' is not valid input");
            }

        }

        System.out.println("Want to do another Calculatoration?");
        repeat = TextIO.getBoolean();   // Retrieves input as a boolean

    }

    System.out.println("Thank you for using my calculator!");

The solution is most likely obvious, but I am not

Jordan.McBride
  • 267
  • 2
  • 7
  • 20

1 Answers1

0

You are never changing corrInput value to false, so it won't never exit the nested while.

corrInput = false;

You may also want to use str1.equals(str2) to compare strings instead of ==.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73