Results 1 to 9 of 9
  1. #1
    fiqueudrue's Avatar
    fiqueudrue is offline Member
    Join Date
    Dec 2008
    Location
    Singapore
    Posts
    55
    Rep Power
    0

    Default Comparing string using == or != (how to compare string in if else)

    how do i compare string in if else ?

    cos if i do these, it get an error of
    Comparing string using == or !=

    if(carModel == "Honda") {
    car.setCarID(1);
    car.setCarModel("Toyota");
    car.setCarDescription("Powered by VVTi Engine");
    car.setCarTransmission("Auto");
    car.setCarPrice(56000);
    Last edited by fiqueudrue; 02-10-2009 at 06:32 AM. Reason: [SOLVED]
    VTEC may be long gone... but the power of MZR is coming...

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Java Code:
    if(carModel.equals("Honda")) {
    car.setCarID(1);
    car.setCarModel("Toyota");
    car.setCarDescription("Powered by VVTi Engine");
    car.setCarTransmission("Auto");
    car.setCarPrice(56000);
    }
    The other method is stringvariable.equalsIgnoreCase("An EXaMpLE");
    For !=, use if(!(stringvar.equals("comparestring")));
    so two methods, object.equals(""), and object.equalsIgnoreCase(""). Use the ! operator for NOT, which gives you opposite, !=. You can only compare primitive data types with ==, >, etc. Strings are Objects and therefore must use the Object's class's equals("") method.
    Last edited by MK12; 02-10-2009 at 01:44 AM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    fiqueudrue's Avatar
    fiqueudrue is offline Member
    Join Date
    Dec 2008
    Location
    Singapore
    Posts
    55
    Rep Power
    0

    Default

    thanks your very much ..

    i did use equal, but the result shows none,
    then i use the .contain("Honda") then will works ...
    is it fine using this way ?
    VTEC may be long gone... but the power of MZR is coming...

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    To compare strings, you use the String.compareTo method.

    Java Code:
    if(carModel.compareTo("Honda") == 0)...
    Also keep in mind, that if these strings are entered by users, they may be upper case, lower case, camel case, etc., so use the toupper ot tolower method of the String class to normalize them before comparison.

  5. #5
    fiqueudrue's Avatar
    fiqueudrue is offline Member
    Join Date
    Dec 2008
    Location
    Singapore
    Posts
    55
    Rep Power
    0

    Default

    have tried it to ..
    thanks u very much!
    cheers
    VTEC may be long gone... but the power of MZR is coming...

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    And you can use...
    if(carModel.equals("Honda")){}
    if(!carModel.equals("Honda")){}
    )))

  7. #7
    fiqueudrue's Avatar
    fiqueudrue is offline Member
    Join Date
    Dec 2008
    Location
    Singapore
    Posts
    55
    Rep Power
    0

    Default

    ic ...
    now i know theres a lot of ways that i can use .....
    alright sure .. but i do believe every way have its own better use of codes ..
    VTEC may be long gone... but the power of MZR is coming...

  8. #8
    rahilgada is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Comparing string using == or != (how to compare string in if else)

    can some tell how do i write this program?
    write a program in java to accept two string input from the user if they are equal then print
    *****
    ****
    ***
    **
    *
    else print the table of 5 i.e. 5,10 15,20 25,...... so on..
    plz help me
    Thank You

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Comparing string using == or != (how to compare string in if else)

    @rahilgada: don't wake up this old thread; you have started your own thread for your question. I'm closing this thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  2. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  3. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  4. Replies: 1
    Last Post: 08-03-2007, 10:52 AM
  5. String Compare not working
    By Revelation in forum New To Java
    Replies: 3
    Last Post: 06-30-2007, 06:43 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
  •