Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Quick Java Problem

  • 26-03-2009 07: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, Registered Users 2 Posts: 378 ✭✭sicruise


    You can also use the apache commons StringUtils method isNotEmpty

    Will work better and will avoid NPE's


  • Registered Users, Registered Users 2 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