### Thread: If() and else() help

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
1
Rep Power
0

#### If() and else() help

This code runs perfect I just need to make it so when dividing if the "number2" is zero, then display an error message; otherwise divide the first number by the second and display the results. Here is what I have so far:

public static void main(String[] args) {
double number1;
double number2;
String a, b;

Scanner input = new Scanner(System.in);

//User Input
System.out.println("Please enter a value for a:");
number1 = input.nextDouble();
System.out.println("Please enter a value for b:");
number2 = input.nextDouble();

System.out.println("List of operations to apply to inputs:");
System.out.println("A: Adds the two numbers");
System.out.println("S: Subtracts the two numbers");
System.out.println("M: Multiplies the two numbers");
System.out.println("D: Divides the two numbers");
System.out.println("Please enter which operation you'd like to use:" );

//Variables
String operationSel;
operationSel = input.next();

//Output
if("a".equals(operationSel) || ("A".equals(operationSel))) {
System.out.print("The sum of your two numbers is"+ (number1+number2));
}

if ("s".equals(operationSel) || ("S".equals(operationSel))) {
System.out.print("The difference of your two number is" + (number1 - number2));
}

if ("m".equals(operationSel) || ("M".equals(operationSel))) {
System.out.print("The product of your two numbers is"+ (number1*number2));
}

if ("d".equals(operationSel) && ("D".equals(operationSel))) {
System.out.print("The quotient of your two numbers is"+ (number1/number2));
//Here is where I need to put in for number2=0
}
}
}
2. if the "number2" is zero, then display an error message;
What problems are you having writing that code?

Please edit your post and wrap the code in code tags to make it easier to read and understand.