Results 1 to 6 of 6
  1. #1
    sheckoo is offline Member
    Join Date
    Oct 2008
    Location
    Egypt
    Posts
    26
    Rep Power
    0

    Exclamation 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. #2
    jayant3001's Avatar
    jayant3001 is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

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

  3. #3
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    Not sure what you are doing with int but, equals() can compare objects.

    cheers

    Ian J

  4. #4
    sheckoo is offline Member
    Join Date
    Oct 2008
    Location
    Egypt
    Posts
    26
    Rep Power
    0

    Default

    i cannot insert after Switch any string it should be integer and after case must be constant!!!!!!!!!

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    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. #6
    jayant3001's Avatar
    jayant3001 is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

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

Similar Threads

  1. Need help with my code.
    By stormviper in forum New To Java
    Replies: 8
    Last Post: 11-18-2013, 03:58 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  3. Pls some one to help mi wit this code
    By _nik_ in forum New To Java
    Replies: 3
    Last Post: 02-10-2008, 02:02 AM
  4. help with oop code
    By nhlfan in forum New To Java
    Replies: 1
    Last Post: 11-27-2007, 08:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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