Results 1 to 4 of 4
  1. #1
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Testing for single string in IF

    I'm running a program and a part of it requires the user to enter Y/N to a question can anyone tell me if this test method is even the slightest bit correct for conducting such a test...

    [cod]
    public static boolean isAnswer(String p){
    if(p=="Y"){
    return true;
    }else{
    return false;
    }//close if-else statement
    }//close isAnswer method
    [/cod]

    I'm simply testing true or false whether the user entered Y to continue the loop of the program or to end it. Thanks alot.

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    Don't use == to compare strings; this compares object references, not the content of the string. Use the equals() method:
    Java Code:
    if(p.equals("Y")) ...

  3. #3
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    IRON LION, thanks much bro...
    That's exactly what I was missing, i'm suprised though I missed something simple as that but I guess when you're deep in heavy code sometimes the small things miss us.

    Thanks again pal...

  4. #4
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

Similar Threads

  1. Testing if it is an Int and if not ask again
    By Alexander_003 in forum New To Java
    Replies: 2
    Last Post: 11-27-2010, 01:09 AM
  2. how to change a single index of a string
    By ftrengnr in forum New To Java
    Replies: 5
    Last Post: 11-26-2010, 05:08 AM
  3. Replies: 2
    Last Post: 03-27-2010, 08:22 PM
  4. Splitting single string into array elements
    By phil128 in forum New To Java
    Replies: 11
    Last Post: 01-12-2009, 12:51 PM
  5. Testing EMF models.
    By manik_jforum in forum Eclipse
    Replies: 0
    Last Post: 12-10-2008, 09:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •