Results 1 to 6 of 6
  1. #1
    ComicStix is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default I can't get my code to work?

    I'm trying to make my first program where if the person inputs the right name (my name :)) they will win the game, but if they don't it will print something else. So in my if then statement which is displayed here:

    if (yes == nicole) {
    System.out.print("You guessed it! What a great name.");
    }

    it refuses to work. I know that when I did my calculator and did if (apples == 10) it worked but here it refuses to work correctly. Can you guys help? Here's the complete code:

    import java.util.Scanner;

    public class name {

    public static void main(String args[]) {
    Scanner yes = new Scanner (System.in);
    System.out.println ("Welcome to the awesome name game!");
    System.out.println ("It's simple. Just guess a name and I'll tell you if it's awesome.");
    System.out.println ("Guess name?");
    System.out.println (yes.nextLine());
    if (yes == nicole) {
    System.out.print("You guessed it! What a great name.");
    }
    else {
    System.out.print("Incorrect. What a silly name!");
    }

    }

    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default

    You don't compare the content of two Strings with the == operator; use the equals( ... ) method instead; you can only compare primitives (int, long, double, char etc) for equality with the == operator.

    kind regards,

    Jos

  3. #3
    ComicStix is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Can you explain further about this equal(...) method? I'm confused what you mean.

  4. #4
    ComicStix is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    O.K I changed my name to this:

    if (yes.equals("nicole")) {
    System.out.print("You guessed it! What a great name.");
    }

    but even when i print in nicole for the input it says i guessed the wrong name when I really did guess the right name.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Next, you will want to read up on how to use a java.util.Scanner object. For one, calling next() gets the next token available and returns the String. My guess is that you want to use nextLine(), not next(), and nextLine also returns a String. but again, you'll want to create a separate String variable to store the String returned:

    Java Code:
    String response = yes.nextLine();
    // then use the response String to test if it equals your key String.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ComicStix View Post
    Can you explain further about this equal(...) method? I'm confused what you mean.
    You should bookmark (or even better download it) Java SE 6 Documentation and read all about all classes and all methods that are at your disposal.

    kind regards,

    Jos

Similar Threads

  1. This code doesn't work. Why? thanks.
    By seanzhang in forum New To Java
    Replies: 10
    Last Post: 07-09-2010, 05:53 AM
  2. Code does not work
    By scotts in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 09:37 AM
  3. Why this code don't work?
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 04-16-2010, 02:57 AM
  4. How does the '*' work in the code?
    By DrMath in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 09:26 AM
  5. the code dosent work
    By pcman in forum Java 2D
    Replies: 1
    Last Post: 03-20-2008, 08:20 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
  •