Results 1 to 5 of 5
  1. #1
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Seems like this should work but it doesn't?

    Okay so this is a logical error. The purpose of this code is to validate input. The only allowable gifts are book, blocks, and plushie; however, no matter what I type, I still get told that it's unavailable. Thanks.

    Java Code:
    System.out.print("What gift do you want to give " + childName + "? ");
    giftChoice = scan.next();
    System.out.print(giftChoice);
    while (giftChoice != "plushie" && giftChoice != "book" && giftChoice != "blocks")
    {
    	System.out.print("That is unavailable, please choose another gift: ");
    	giftChoice = scan.next();
    }
    		
    toy.setToy(giftChoice);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Seems like this should work but it doesn't?

    You need to use the equals() method to compare the contents of objects like Strings, not == or !=.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Seems like this should work but it doesn't?

    Okay, I changed it to:

    Java Code:
    System.out.print("What gift do you want to give " + childName + "? ");
    giftChoice = scan.next();
    		
    while (!(giftChoice.equalsIgnoreCase("plushie") && giftChoice.equalsIgnoreCase("book") && giftChoice.equalsIgnoreCase("blocks")))
    {
    	System.out.print("That is unavailable, please choose another gift: ");
    	giftChoice = scan.next();
    }
    It still keeps saying whatever I type in is not a valid input.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Seems like this should work but it doesn't?

    With a compound expression made of multiple simple boolean expressions connected by AND operators, the first false condition will make the compound expression false.
    Not false = true
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Why doesn't this work
    By The Doctor11 in forum New To Java
    Replies: 11
    Last Post: 11-04-2014, 06:57 PM
  2. I wonder why this doesn't work...
    By louboulos in forum New To Java
    Replies: 3
    Last Post: 05-13-2012, 05:59 PM
  3. Why doesn't this work?
    By mailman in forum Java Applets
    Replies: 5
    Last Post: 01-10-2012, 02:01 PM
  4. Jar doesn't work
    By mad72584 in forum New To Java
    Replies: 35
    Last Post: 08-07-2011, 06:22 PM
  5. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 11:33 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
  •