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

Quick Java Problem

Options
  • 26-03-2009 7:41pm
    #1
    Closed Accounts Posts: 20


    I am recieving input from the user, and then I want to check if they actually entered anything:

    String searchValue = JOptionPane.showInputDialog(this, "Enter the node value you are looking for");


    //Check that the user entered something
    if(searchValue != "")
    {

    But that if statement returns true even if I leave the text box empty. What am I doing wrong?

    Cheers


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    You are comparing Objects, not strings. Looks into the String equals method.


  • Closed Accounts Posts: 20 Whackhead


    You are comparing Objects, not strings. Looks into the String equals method.

    Cheers, had forgotten about that.


  • Registered Users Posts: 378 ✭✭sicruise


    You can also use the apache commons StringUtils method isNotEmpty

    Will work better and will avoid NPE's


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


    sicruise wrote: »
    You can also use the apache commons StringUtils method isNotEmpty

    Why pull in a whole library for 1 line of code?

    s != null && !s.equals("")


Advertisement