# Java calculator using swing and applet

• July 29th, 2013, 01:39 AM
lordrey
Java calculator using swing and applet
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: ");
JOptionPane.showInputDialog("Enter second number: ");

if((choice<=5) && (choice>0))
{

switch (choice)
{

System.out.println(x + " plus " + y + " = " + (x + y) );
break;

case 2: //subtraction no decimal
System.out.println(x + " minus " + y + " = " + (x - y) );
break;

case 3: //multiplication 3 decimal
System.out.println(x + " times " + y + " = " + (x * y) );
break;

case 4: //division 4 decimal
System.out.println(x + " divided by " + y + " = " + (x / y) );
break;

case 5: //modulo
System.out.println(x + " modulo " + y + " = " + (x % y) );
//break;
}

}
else
{
System.out.println("Please enter a 1, 2, 3, 4 or 5.");
}
}
}

--- Update ---

please help me solve my code its for my final exam. the code doesn't continue to its input number
• July 29th, 2013, 05:58 AM
syedbhai
Re: Java calculator using swing and applet
Hello.
You are first reading the operation using JOptionpane. So you don't need Scanner anymore.
Just replace the scanner code with this line. It should work.

int choice=Integer.parseInt(input);

Thats it.

Thanks,
Syed.
• July 29th, 2013, 08:40 PM
lordrey
Re: Java calculator using swing and applet
Quote:

Originally Posted by syedbhai
Hello.
You are first reading the operation using JOptionpane. So you don't need Scanner anymore.
Just replace the scanner code with this line. It should work.

int choice=Integer.parseInt(input);

Thats it.

Thanks,
Syed.

tons of thanks sir. now my problem is how to convert those int to float and displays required decimal places.
• July 29th, 2013, 11:19 PM
jps
Re: Java calculator using swing and applet
Quote:

Originally Posted by lordrey
tons of thanks sir. now my problem is how to convert those int to float and displays required decimal places.

What did you try?
Your homework is meant for you to figure out, to give you the ability to solve the problem. Not to see how someone on a forum would solve the problem.