I don't know what I am missing.
Help me.

import javax.swing.*;
import java.util.Scanner;

public class Operator {

  public static void main(String args[])
  {
   String num1,num2,op;
   int ans;

   Scanner cin = new Scanner(System.in);


   num1 = JOptionPane.showInputDialog("Enter First Number");
   op = JOptionPane.showInputDialog("Enter Operator");

   num2 = JOptionPane.showInputDialog("Enter Second Number");
   char c = (char) Integer.parseInt(op);
   int fnum = Integer.parseInt(num1);
   int snum = Integer.parseInt(num2);

   switch(c)
   {
     case '+':

    ans = fnum + snum;
    JOptionPane.showMessageDialog(null,"Answer" + ans);

    break;
     case '*':

    ans = fnum * snum;
    JOptionPane.showMessageDialog(null,"Answer" + ans);

    break;
     case '-':

    ans = fnum - snum;
    JOptionPane.showMessageDialog(null,"Answer" + ans);

    break;
     case '/':

    ans = fnum/snum;
    JOptionPane.showMessageDialog(null,"Answer" + ans);

    break;

     default:

     JOptionPane.showMessageDialog(null, "Wrong Operator");
   }
  }


         }

Line 18. an String for (+,-,*,/) cant be cast for an Integer, there is not a number. im not sure what are an correctly method for make an cast. but this is succefully. replace in the line 18. for this.
char c = op.charAt(0);
is all ! ;) have a nice day.

since Java 7, u can use an String in your switch! ;)
only you need use this in ur switch sentence.

  switch(op){
         case "+": ans = fnum + snum;
        JOptionPane.showMessageDialog(null,"Answer" + ans);break;
        //all of remains opetarions.
        }

Edited 2 Years Ago by zolymo: any aclaration

This article has been dead for over six months. Start a new discussion instead.