Results 1 to 8 of 8
  1. #1
    daletron3030 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default Can't get my "if" statement to read user input

    So i type "n" when i run the program and when i put the println of cmd is equal to it's properly stores "n" in cmd it seems like.. but i can't get the if statement of "if(cmd=="n") to go through... any tips?

    String cmd=null;
    Scanner kb = new Scanner(System.in);

    System.out.print("Command? ");
    cmd = kb.nextLine();

    System.out.println("cmd = '" + cmd + "'"); //test purposes
    if(cmd=="n")
    {
    System.out.println("working");
    System.out.println(listnames.randomname() + "\n");
    }

  2. #2
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    I think you should try using another comparison method for the String. like .equals() or compareTo().

    but Allah knows best

  3. #3
    daletron3030 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    thank you that worked =)

    used to C++, pretty new to java and having some trouble with a few basic stuff like that

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

    Default

    the thing to know is that String is an object, and when you compare objects by using equality, ==, you are checking to see if the two objects are one and the same, not that the values they hold are the same. You really don't care if one String is the exact same object as another String here. Instead you care that they both hold the same value, "n". Therefore, you must use the .equals(...) or .equalsIgnoreCase(...) methods.

    make sense?

  5. #5
    daletron3030 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    yea makes sense, thanks for breaking it down.

    i would think though that if both strings contained the same value inside, that they indeed would be one and the same? so i guess that would only work is if i did something like..

    string a = "blah";
    string b;
    b = a;
    if(b==a) system.out.print("true");

    am i correct in all this?

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

    Default

    try this:
    Java Code:
      String string1 = new String("Fubar");
      String string2 = new String("Fubar");
      System.out.println("Using == : " + (string1 == string2));
      System.out.println("Using equals() : " + (string1.equals(string2)));

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    right, because you have to be careful of interned strings.
    so:
    String s1 = "foobar";
    String s2 = "foobar";

    could end up being the same. but:
    String s1 = new String("foobar");
    String s2 = new String("foobar");

    would not be the same.

    btw. whats fubar? i thought it was foobar. google for fubar, you get some funny answers. LOL

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

Similar Threads

  1. trouble with Scanner(new File("input"));
    By ronyosi in forum New To Java
    Replies: 9
    Last Post: 10-27-2010, 11:34 PM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  3. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  4. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 01:20 AM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 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
  •