Results 1 to 12 of 12

Thread: String?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    String?

    Ok i remember my teacher saying something about useing a string in a if but i dont remember what it is :-/

    this isnt working ???? Why?


    Code:
    if(strName != " ")
          {
           lblName.setText(strName);
          }
          else
          {
            //Do Something
            lblError.setText("Error1");
          }

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    When doing Object comparisions, the normal equality operators(==, !=, etc) will only compare whether or not the variables point to the same object. To actually compare values, use the equals() method.
    Code:
    if(!strName.equals(" "))
    {
        lblName.setText(strName);
    }
    else
    {
        //Do Something
        lblError.setText("Error1");
    }
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    Thanks Much

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    hmm that still didnt work :-/

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Are you getting errors, or are the results just not as you expected?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    I can't see how this has any errors in it. I've always used the String.equals("") method.

    Does this throw an exception when the string inside the equals() is longer than the string itself? e.g.

    Code:
    String thisString = "";
    
    if(!thisString.equals(" "))
    {
      ....
    }
    This might throw an exception..?

    HD

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Would be very strange if it did, I would consider it a bug.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    im gettting a single result if its blank its still displaying the true result

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    heres my Code just to give a ref
    Code:
      public void Check()
      {
          strName = txtName.getText();
          strAddress = txtAddress.getText();
          strCC = txtCC.getText();
          int intQuantity = Integer.parseInt(txtQuantity.getText());
           if(!strName.equals(" "))
           {      //Validation myVal = new Validation(intQuantity);
            lblTitle.setText("Hello");
           }
           else
           {
            lblTitle.setText("GoodBy");
           }
      }

  10. #10
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Are you looking for a string that is a single space, or an empty string? Because your code is looking for a string that is a single space. Try
    Code:
    if (strName.length() > 0)
    {
        lblTitle.setText("Hello");
    }
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  11. #11
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Posted by CornedBee
    Would be very strange if it did, I would consider it a bug.
    Sorry, I was thinking of String.substring() which throws an exception when you do something like:

    Code:
    String str = "hello";
    
    if(str.substring(0, 7).equals("goodbye"))
    {
    .......
    }
    Thats what I was confusing it with. Obviously this would cause an exception as its trying to access string indexes that dont exist.

    HD

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    Ok it Works now thanks alot!!
    Last edited by 308holes; Oct 31st, 2002 at 01:23 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
  •  



Click Here to Expand Forum to Full Width