Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java calculator not executing if-statement [duplicate]

I'm relatively new to programming and have recently started learning Java in order to move into Android programming. I thought I would create a very simple calculator to practice, but it seems that my if statement doesn't work.

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        //Create new scanner object
        Scanner numInput = new Scanner( System.in );

        //Enter first number
        System.out.println("Please enter the first number: ");
        int num1 = numInput.nextInt();

        //Enter the second number
        System.out.println("Please enter the second number: ");
        int num2 = numInput.nextInt();

        //Choose the operation to perform (+,-,*,/)
        System.out.println("What operation would you like to do?");
        System.out.println("Type \"+\" to add.");
        System.out.println("Type \"-\" to subtract.");
        System.out.println("Type \"*\" to multiply.");
        System.out.println("Type \"/\" to divide.");
        String opChoice = numInput.nextLine();


        //Add
        if (opChoice.equals("+")) {
            int ans = num1 + num2;
            System.out.println("Adding " + num2 + " to " + num1 + " equals " + ans + ".");
        }

        //Subtract
        else if (opChoice.equals("-")) {
            int ans = num1 - num2;
            System.out.println("Subtracting " + num2 + " from " + num1 + " equals " + ans + ".");
        }

        //Multiply
        else if (opChoice.equals("*")) {
            int ans = num1 + num2;
            System.out.println("Multiplying " + num2 + " with " + num1 + " equals " + ans + ".");
        }

        //Divide
        else if (opChoice.equals("/")) {
            int ans = num1 + num2;
            System.out.println("Dividing " + num1 + " by " + num2 + " equals " + ans + ".");
        }

    }

}

I am using the Eclipse IDE, and it runs fine until it asks for which operation to do. It will display the options but won't let me enter anything (I've been testing it with multiplying 5 by 2).

I searched for similar questions and tried what they suggested, but it still doesn't seem to work. I would appreciate any help, I assume this is probably just some simple error I am making, so I apologize if this seems like a silly question!

EDIT: Thanks for the quick responses, guys! I appreciate it. And yes, I fixed the multiply and division. :)

like image 586
Qwurticus Avatar asked Mar 21 '23 22:03

Qwurticus


2 Answers

The problem is that nextInt() doesn't consume (doesn't read) the new-line character (that you input when you press [Enter]). One way to solve this is calling nextLine() after each nextInt():

//Enter first number
System.out.println("Please enter the first number: ");
int num1 = numInput.nextInt();
numInput.nextLine(); // Add this

//Enter the second number
System.out.println("Please enter the second number: ");
int num2 = numInput.nextInt();
numInput.nextLine(); // Add this

Another way to solve this, would be reading the numbers with nextLine() (which returns a String) and then parsing it to a int:

int num1 = Integer.parseInt(numInput.nextLine());

You won't need to add an extra nextLine() because the new-line character is being consumed by the nextLine() already called.

Also, as @sotondolphin suggested, you may want to check your * and / operations.

like image 118
Christian Tapia Avatar answered Mar 23 '23 11:03

Christian Tapia


The issue is that when numInput.nextInt(); is called, you get the number entered ... but it leaves the newline (\n). Your call to numInput.nextLine(); then gets an empty string.

Replacing that call with numInput.next() will solve the problem as it has a slightly different behavior:

public String next()

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

The default delimiter pattern is whitespace, which includes \n and what's in the input stream after you enter the operation (using * as example) is now \n*\n

like image 33
Brian Roach Avatar answered Mar 23 '23 12:03

Brian Roach