Results 1 to 5 of 5
Thread: calculator-Switch not invoked
- 11-19-2012, 10:01 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
calculator-Switch not invoked
Hi dear friends I am making my class assignment to make a basic calculator,in which the user can give value in run time and can select the function which he/she want to perform. My code is given below
XML Code://Simple calculator. import javax.swing.*; class calculator{ int val1; int val2; int choice; } class cal{ public static void main(String args[]){ calculator x; x=new calculator(); String abc=JOptionPane.showInputDialog(null, " Insert value 1"); String def=JOptionPane.showInputDialog(null, " Insert Value 2"); String op="Select the choice:\n 1 for Addition.\n 2 for subtraction.\n 3 for multiplication.\n 4 for division."; String ghi=JOptionPane.showInputDialog(null, op); x.val1=Integer.parseInt(abc); x.val2=Integer.parseInt(def); x.choice=Integer.parseInt(op); switch(x.choice){ case 1: int s; s=x.val1+x.val2; JOptionPane.showMessageDialog(null, "Total Sum is= "+s); break; case 2: int multi; multi=x.val1*x.val2; JOptionPane.showMessageDialog(null, "Multiplication ans= "+multi); break; case 3: int div; div=x.val1/x.val2; JOptionPane.showMessageDialog(null, "Division ans= "+div); break; default: JOptionPane.showMessageDialog(null, "invalid choice"); } } }
Please reply quickly.
- 11-19-2012, 10:16 AM #2
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0
Re: calculator-Switch not invoked
Let me fix that coding first.. hard to look at.
Java Code://Simple calculator. import javax.swing.*; class calculator{ int val1; int val2; int choice; } class cal{ public static void main(String args[]){ calculator x; x=new calculator(); String abc=JOptionPane.showInputDialog(null, " Insert value 1"); String def=JOptionPane.showInputDialog(null, " Insert Value 2"); String op="Select the choice:\n 1 for Addition.\n 2 for subtraction.\n 3 for multiplication.\n 4 for division."; String ghi=JOptionPane.showInputDialog(null, op); x.val1=Integer.parseInt(abc); x.val2=Integer.parseInt(def); x.choice=Integer.parseInt(op); switch(x.choice){ case 1: int s; s=x.val1+x.val2; JOptionPane.showMessageDialog(null, "Total Sum is= "+s); break; case 2: int multi; multi=x.val1*x.val2; JOptionPane.showMessageDialog(null, "Multiplication ans= "+multi); break; case 3: int div; div=x.val1/x.val2; JOptionPane.showMessageDialog(null, "Division ans= "+div); break; default: JOptionPane.showMessageDialog(null, "invalid choice"); } } }
Java Code:String ghi=JOptionPane.showInputDialog(null, op);
Java Code:x.choice=Integer.parseInt(ghi);
Last edited by Jingoism; 11-19-2012 at 10:34 AM.
- 11-19-2012, 11:00 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Re: calculator-Switch not invoked
Thanks for reply Friend.
I did what you suggest to do with this code but it does not compile and mark a new issue, Please check it again practically I shall be very thankful to you.
- 11-19-2012, 11:14 AM #4
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Re: calculator-Switch not invoked
OH Thanks God I got solution and Now the fully function code is
XML Code://Simple calculator. import javax.swing.*; class calculator{ int val1; int val2; int choice; } class cal{ public static void main(String args[]){ calculator x; x=new calculator(); String abc=JOptionPane.showInputDialog(null, " Insert value 1"); String def=JOptionPane.showInputDialog(null, " Insert Value 2"); String op="Select the choice:\n 1 for Addition.\n 2 for subtraction.\n 3 for multiplication.\n 4 for division."; String ghi="0"; ghi=JOptionPane.showInputDialog(op, ghi); x.val1=Integer.parseInt(abc); x.val2=Integer.parseInt(def); x.choice=Integer.parseInt(ghi); switch(x.choice){ case 1: int a; a=x.val1+x.val2; JOptionPane.showMessageDialog(null, "The Sum is= "+a); break; case 2: int s; s=x.val1-x.val2; JOptionPane.showMessageDialog(null, "The subtraction is= "+s); break; case 3: int m; m=x.val1*x.val2; JOptionPane.showMessageDialog(null, "The Multiplication is= "+m); break; case 4: int d; d=x.val1/x.val2; JOptionPane.showMessageDialog(null, "The Division is= "+d); break; default: JOptionPane.showMessageDialog(null, "invalid choice"); } } }
- 11-19-2012, 11:45 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0
Re: calculator-Switch not invoked
What error did you get? I just pasted the code you had up top, and replaced the "op" with "ghi" and it ran perfectly.
String javax.swing.JOptionPane.showInputDialog(Component parentComponent, Object message)... I guess it works, and that is all that matters.Last edited by Jingoism; 11-19-2012 at 11:48 AM.
Similar Threads
-
Servlet URL from JSP IMG not invoked second time
By satishgauns in forum Java ServletReplies: 1Last Post: 11-19-2012, 11:40 AM -
Get Invoked Methods BCEL
By Astrid in forum Advanced JavaReplies: 1Last Post: 05-18-2012, 06:56 PM -
Calculator using Switch
By pinkdreammsss in forum New To JavaReplies: 17Last Post: 04-13-2012, 03:19 PM -
Concurrent timers are not getting invoked for same timer info (Serializable object co
By Neeraj in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-31-2008, 03:20 PM -
Switch help please!!!!
By soc86 in forum New To JavaReplies: 6Last Post: 11-23-2008, 08:25 PM
Bookmarks