# Thread: Help me with this code???

1. Member
Join Date
Oct 2008
Location
Egypt
Posts
26
Rep Power
0

## 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;

}
)))))))

2. why u r trying to compare string and integer?
take (+,-,*,/) as string n then compare .it will work.
dont take plus as int.

3. Member
Join Date
Oct 2008
Posts
21
Rep Power
0
Not sure what you are doing with int but, equals() can compare objects.

cheers

Ian J

4. Member
Join Date
Oct 2008
Location
Egypt
Posts
26
Rep Power
0
i cannot insert after Switch any string it should be integer and after case must be constant!!!!!!!!!

5. I don't understand what your problem is? Can you explain?
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.

6. one easy way to do is like this:
Java 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;

}

#### Posting Permissions

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