Results 1 to 5 of 5
  1. #1
    sami205 is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default 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"); 
     }
      }
    }
    when I run this program only Input dialogbox open and take values from me but not performed any function as all function written in switch statements and its not invoked please help me where I did something wrong in this code or what is missing in this code?
    Please reply quickly.

  2. #2
    Jingoism is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default 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"); 
    		}
    	}
    }
    Problem is here:

    Java Code:
    String ghi=JOptionPane.showInputDialog(null, op);
    Replace op with ghi and you are gold.

    Java Code:
    x.choice=Integer.parseInt(ghi);
    You just got a bit backward on which var to use.
    Last edited by Jingoism; 11-19-2012 at 10:34 AM.

  3. #3
    sami205 is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  4. #4
    sami205 is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default 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");
     }
      }
    }

  5. #5
    Jingoism is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default 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

  1. Servlet URL from JSP IMG not invoked second time
    By satishgauns in forum Java Servlet
    Replies: 1
    Last Post: 11-19-2012, 11:40 AM
  2. Get Invoked Methods BCEL
    By Astrid in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2012, 06:56 PM
  3. Calculator using Switch
    By pinkdreammsss in forum New To Java
    Replies: 17
    Last Post: 04-13-2012, 03:19 PM
  4. Replies: 0
    Last Post: 12-31-2008, 03:20 PM
  5. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 08:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •