-1

I'm trying to teach my friend Java. I tried this simple calculator.

public static void main(String[] args) {

    boolean powerOn = true;

    while(powerOn) {
        @SuppressWarnings("resource")
        Scanner userInput = new Scanner(System.in);

        System.out.println("Welcome to the calculator\nPlease enter a number (Enter 3.14 for PI)"); 
        double firstNumber = userInput.nextDouble();

        if(firstNumber == 3.14) firstNumber = Math.PI;

        System.out.println("Please enter an operation(+,-,*,/, Square Root)");
        String operation = userInput.next();

        if(operation.equalsIgnoreCase("Square Root")) System.out.println(Math.sqrt(firstNumber));

        else {
            System.out.println("Please enter another number");
            double secondNumber = userInput.nextDouble();

            if(secondNumber == 3.14) secondNumber = Math.PI;

            if (operation.equals("+")) {
                if(firstNumber == 9 && secondNumber == 10) System.out.println("21");
                else System.out.println(firstNumber+secondNumber); 
            }
            else if (operation.equals("-")) System.out.println(firstNumber-secondNumber);
            else if (operation.equals("*")) System.out.println(firstNumber*secondNumber);
            else if (operation.equals("/")) System.out.println(firstNumber/secondNumber);
        }

        System.out.println("Power off?");
        String off = userInput.next();

        if(off.contains("y")) System.exit(1);
    }
}

If you do square root, it prints enter another number and then throws an exception... I know what's happening, but why and how do I prevent it?

And just a side note can someone explain to me the difference between Scanner#next() and Scanner#nextLine?

PM 77-1
  • 11,712
  • 18
  • 56
  • 99
Zach
  • 19
  • 3

3 Answers3

0

next() returns the string before the space while nextLine() as you might have guessed returns the whole line .

You are probably also getting the error because of the same reason as your string "Square Root" has a space and hence operation variable always have "Sqaure" value assigned to it rather than "Square Root".

This pushes it to the else block instead of if.

Maddy
  • 123
  • 4
0

You need to update the following code

String operation = userInput.next();

with following

String operation = userInput.nextLine();

The reason being the next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line.

In your current code, when you try to get the square root, the operation will just store "Square" instead of "Square Root" and therefore the following if condition isn't satisfied

if(operation.equalsIgnoreCase("Square Root")) 

and the code goes into else condition and asks for another number and tries to process it and in the else block it throws error on this line

double secondNumber = userInput.nextDouble();

Apart from changing the next() to nextLine() in your code, you need to update following

double firstNumber = userInput.nextDouble();

with

double firstNumber = userInput.nextDouble();    
userInput.nextLine();

to ensure that the scanner doesn't skip your nextLine() because in nextDouble() method only the double is consumed and the next line characters (\n) aren't.

Similarly update

double secondNumber = userInput.nextDouble();

with

double secondNumber = userInput.nextDouble();
userInput.nextLine();

For more info about skipping of nextLine() method while using nextDouble() refer this link.

Community
  • 1
  • 1
Balwinder Singh
  • 2,196
  • 5
  • 20
  • 32
0

To fix your issue add a scan.nextLine(); Before you scan in the operation. And while scanning in the operation use a scan.nextLine(); as well instead of the scan.next();

Think of the scan as the location of the cursor. A scan.nextDouble(); will place the cursor after the last digit of the number, and a scan.nextLine(); will scan in what ever is inbetween the cursor's location and the beginning of the next line. scan.next(); won't scan in the entire line.

brettfazio
  • 906
  • 8
  • 23