Friday 2 December 2016

Java calculator not executing if-statement





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. :)


Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...