# Help me with this code???

• 10-30-2008, 07:16 PM
sheckoo
Help me with this code???
in this code i want to compare between 2 integers look and u will understand it

(((((

String o=
JOptionPane.showInputDialog("please enter the kind of operation (+,-,*,/,%");
int plus=Integer.parseInt("+");
int minus=Integer.parseInt("-");
int mult=Integer.parseInt("*");
int divide=Integer.parseInt("/");
int mod=Integer.parseInt("%");

switch(Integer.parseInt(o))
{
case plus: // iwant to compare between plus and o
JOptionPane.showMessageDialog(null, "the value = "+(x+y));
break;

}
)))))))
• 10-30-2008, 07:30 PM
jayant3001
why u r trying to compare string and integer?
take (+,-,*,/) as string n then compare .it will work.
dont take plus as int.
• 10-30-2008, 07:36 PM
ianjedi
Not sure what you are doing with int but, equals() can compare objects.

cheers

Ian J
• 10-30-2008, 07:40 PM
sheckoo
i cannot insert after Switch any string it should be integer and after case must be constant!!!!!!!!!
• 10-30-2008, 08:25 PM
Norm
I don't understand what your problem is? Can you explain?
Quote:

iwant to compare between plus and o
if plus is int and o is a String, then you must convert one of them to the same type to be able to compare them. You've done that with the parseInt method. Now they are both int.
ints can be compared with the ==, <, > etc operators.
• 10-30-2008, 08:26 PM
jayant3001
one easy way to do is like this:
Code:

```String o= JOptionPane.showInputDialog("please enter the kind of operation (1 for +,2 for -,3 for *,4 for /,5 for %)");                     switch((Integer.parseInt(o)))     {     case 1: // iwant to compare between plus and o     JOptionPane.showMessageDialog(null, "the value ="+(x+y));     break;     }```