# JAVA Calculator Problem

• July 29th, 2013, 09:44 PM
lordrey
JAVA Calculator Problem
Please Help me solve my problem in using switch and also the answer would be output required decimal places. its for my final exam. thanks a lot.

Code :

```import java.io.*; import java.util.*; import javax.swing.JOptionPane;   public class exercise1 {   public static void main(String args[]){ String input = JOptionPane.showInputDialog("Selection from the choices below:\n\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n 5. Modulo\n\n Your choice?");     JOptionPane.showInputDialog("\nEnter first number: "); int x = Integer.parseInt(x); JOptionPane.showInputDialog("Enter second number: "); int y = Integer.parseInt(y);     int choice = Integer.parseInt(input); if((choice<=5) && (choice>0)) {   switch (choice) {   case 1: //addition 1 decimal place JOptionPane.showMessageDialog(null, x + " plus " + y + " = " + (x + y); break;   case 2: //subtraction no decimal place JOptionPane.showMessageDialog(x + " minus " + y + " = " + (x - y) ); break;   case 3: //multiplication 3 decimal place JOptionPane.showMessageDialog(x + " times " + y + " = " + (x * y) ); break;   case 4: //division 4 decimal place JOptionPane.showMessageDialog(x + " divided by " + y + " = " + (x / y) ); break;   case 5: //modulo JOptionPane.showMessageDialog(x + " modulo " + y + " = " + (x % y) ); //break; }   } else { System.out.println("Please enter a 1, 2, 3, 4 or 5."); } } }```
• July 29th, 2013, 11:22 PM
jps
Re: JAVA Calculator Problem
Quote:

Originally Posted by lordrey

What is your problem in using switch?
Does it compile? Is there an error?

If you need help, ask a question besides the implied "give me a working solution"
• July 30th, 2013, 12:33 AM
syedbhai
Re: JAVA Calculator Problem
Hello.
Thats simple.
If you want floating or double values then change the type of the variables x and y respectively. You will have to use the parseXxx() of the corresponding wrapper class.
In simpler words the way you are doing for integers the same way you do for floats/doubles.

Thanks,
Syed.