-1

I am struggling to get the correct scope for my variable "input".

I am making a calculator for a university task, and I've got everything working apart from when I tried to make it loop by wrapping my main code in a do-while loop. Because the variable "input" is declared in the "do" part of the loop, it didn't know what it was when I was trying to use it in the "while" condition. To fix this I then declared "input" as a string before my do-while loop to make it a global. However, now the scanner that takes the value of input will not work. AM I doing something stupid or am I missing something?

import java.util.Scanner;

public class Calculator {

    public static void main(String [] args) {

        String input;

        do {

            System.out.println("Welcome to the calculator. Please enter an operator (+, -, /, *) below:");

            Scanner myScanner = new Scanner(System.in);
            String oper = myScanner.nextLine();

            System.out.println("Now please enter two numbers:");
            double a = myScanner.nextDouble();
            double b = myScanner.nextDouble();

            switch (oper) {
            case "+" :
                System.out.println(CalculatorUtils.add(a, b));
                break;
            case "-" :
                System.out.println(CalculatorUtils.subtract(a, b));
                break;
            case "/" :
                System.out.println(CalculatorUtils.divide(a, b));
                break;
            case "*" :
                System.out.println(CalculatorUtils.multiply(a, b));
                break;
            }

        System.out.println("Do you want to complete another calculation? (y/n)");
        input = myScanner.nextLine();


        }
        while (input.contentEquals("y"));

    }


}

I expect this to be the output: Welcome to the calculator. Please enter an operator (+, -, /, *) below: + Now please enter two numbers: 32.5 12.5 45.0 Do you want to complete another calculation? (y/n) y (This is where the code would start again)

However I'm not being able to enter my input when being asked if I would like to do another calculation.

August Lilleaas
  • 51,168
  • 11
  • 94
  • 107

2 Answers2

1

Here is the fix.

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        String input;

        do {

            System.out.println("Welcome to the calculator. Please enter an operator (+, -, /, *) below:");

            Scanner myScanner = new Scanner(System.in);
            String oper = myScanner.nextLine();

            System.out.println("Now please enter two numbers:");
            double a = myScanner.nextDouble();
            double b = myScanner.nextDouble();

            switch (oper) {
                case "+":
                    System.out.println(CalculatorUtils.add(a, b));
                    break;
                case "-":
                    System.out.println(CalculatorUtils.subtract(a, b));
                    break;
                case "/":
                    System.out.println(CalculatorUtils.divide(a, b));
                    break;
                case "*":
                    System.out.println(CalculatorUtils.multiply(a, b));
                    break;
            }
            myScanner.nextLine();
            System.out.println("Do you want to complete another calculation? (y/n)");
            input = myScanner.nextLine();
myScanner.nextLine();


        }
        while (input.contentEquals("y"));

    }


}

It happens because second time you call myScanner.nextLine() it just scans enter from before. It will happen after myScanner.nextDouble() but not after myScanner.nextLine() because myScanner.nextLine() reads/scans until including next newLine character (\n) whereas myScanner.nextDouble() will just scan a double and leave.

Here is similar thread

Kshitij Dhakal
  • 620
  • 5
  • 16
0

What you do not want to do is create a Scanner on every trip around the loop. Move the definition and initialization of your Scanner variable outside the loop:

    String input;
    Scanner myScanner = new Scanner(System.in);
    do {
        System.out.println("Welcome to the calculator. Please enter an operator (+, -, /, *) below:");

        String oper = myScanner.nextLine();

        // rest of loop...

    } while (input.contentEquals("y"));

This may or may not solve you're immediate problem, but it's still the right thing to do in general.

Kevin Anderson
  • 4,388
  • 2
  • 10
  • 20
  • Thank you, I have moved it outside of the do-while loop. It hasn't solved my problem, but it had improved my best practise – Olivia Kelly Oct 23 '19 at 12:16
  • I think you need to do `myScanner.nextLine()` after reading in the values of `a` and `b`. The explanation for why is somewhat technical and tedious, but it should help. – Kevin Anderson Oct 23 '19 at 12:30