Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java - Comparing Strings

  • 01-04-2006 2:23pm
    #1
    Registered Users, Registered Users 2 Posts: 3,282 ✭✭✭


    /*
    Below is what im stuck on.
    ******
    String semiColon = ";";

    if (tempString2 == semiColon )
    {
    System.out.print("test");
    tempString2 = " ";
    }
    ******

    In my mind it should compare the tempString2 with a ;. If it finds
    that it is the same as a ; then it should set tempString to a blank
    space. I want it to do this so that it knows if its a double figure
    or if its a single figure.

    EDITED: Solved.


Comments

  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma




  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    EDIT : What he said


  • Registered Users, Registered Users 2 Posts: 44,195 ✭✭✭✭Basq


    I always use String.equalsIgnoreCase myself!


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Have you tried the following?
    if (tempString2.equals(semiColon) )
    

    Edit: or any of the other above suggestions...


  • Registered Users, Registered Users 2 Posts: 3,282 ✭✭✭BlackWizard


    Oh bugger your right.

    I just copied that compare statement from sudo code we made up a few
    days ago and totally messed myself up.

    heh! Thanks guys! I hadnt got much time to look into it either thats why I
    posted it here. :)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    basquille wrote:
    I always use String.equalsIgnoreCase myself!

    WASTEFUL, in this case. Tsk. :)


  • Registered Users, Registered Users 2 Posts: 3,225 ✭✭✭JackKelly


    Why is it that some types can only use "==" and others .equals(...)?


  • Registered Users, Registered Users 2 Posts: 1,997 ✭✭✭The_Bullman


    TimAy wrote:
    Why is it that some types can only use "==" and others .equals(...)?

    I think you can only use the "==" for comparing primitave types. For other classes the equals method should be used

    I think..


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    I think you can only use the "==" for comparing primitave types. For other classes the equals method should be used

    I think..

    Correct. A string is an object not a primative.


  • Closed Accounts Posts: 51 ✭✭garrethg


    TimAy wrote:
    Why is it that some types can only use "==" and others .equals(...)?
    The comparison operator is used to test for equality between both primatives and references to objects.

    e.g.

    String x= new String("some text");
    String y= new String("some text");
    String z= x;

    /* a is false, it's a comparison between an object and a primative. */
    boolean a= x == "some text";
    /* b is true, it's a comparison between the primative value of an object field, returned by the method "equals", and a primative. */
    boolean b= x.equals("some text");
    /* c is false, it's a comparison between references to different objects.*/
    boolean c= x == y;
    /* d is true, it's a comparison between references to the same object. */
    boolean d= x == z;


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    garrethg wrote:
    /* b is true, it's a comparison between the primative value of an object field, returned by the method "equals", and a primative. */
    boolean b= x.equals("some text");

    Eh? Equals returns a boolean, obtained by comparing the contents of x to the string literal "some text", through a mechanism which, I believe, is implementation-defined. (But in practice almost certainly simply involves comparing two arrays of characters).


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    garrethg wrote:
    /* c is false, it's a comparison between references to different objects.*/
    boolean c= x == y;

    Thats guaranteed in this case because you have created two new objects but if you did something like

    String x= "some text";
    String y= "some text";

    then x == y could return true, because of how Java handles strings it sees both strings as one object.

    [edit]
    Just to add. You shouldn't be creating your strings like...

    String x = new String("some text");

    As this is wasteful because you are creating two objects.


  • Closed Accounts Posts: 51 ✭✭garrethg


    rsynnott wrote:
    Eh? Equals returns a boolean, obtained by comparing the contents of x to the string literal "some text", through a mechanism which, I believe, is implementation-defined. (But in practice almost certainly simply involves comparing two arrays of characters).
    Thanks, I don't know what I (wasn't) thinking at the time :o


  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    Hobbes wrote:
    Just to add. You shouldn't be creating your strings like...

    String x = new String("some text");

    As this is wasteful because you are creating two objects.

    AFAIR, modern Javas optimise this, treating String x as a reference.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    rsynnott wrote:
    AFAIR, modern Javas optimise this, treating String x as a reference.

    well it would be a reference but it would also create a reference for "some text" within the method.

    As you said they may do away with this, but its still messy imho.


Advertisement