Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: (if) Statement is not working, even when the condition is true. The (else) statement is working though.

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default (if) Statement is not working, even when the condition is true. The (else) statement is working though.

    [code = java]

    import static java.lang.System.out;
    import java.util.Scanner;

    public class PasswordChecker {

    public static void main (String args []) {

    Scanner PasswordInput = new Scanner (System.in);
    String Password = PasswordInput.next ();

    out.println("You typed >>>"+Password+"<<<");

    out.println();

    if (Password == "Nathan") {

    out.println("The word you typed is stored ");
    out.println("in the same place as the ");
    out.println("real password!");
    out.println("You must be a hacker!");
    } else {

    out.println("The word you typed is ");
    out.println("not stored in the same place ");
    out.println("as the real password!");
    out.println("But that is okay!");
    }

    out.println ();

    if (Password.equals ("Nathan")) {

    out.println("The word you typed has the same ");
    out.println("characters as the real password");
    out.println("You can use our precious System");
    } else {

    out.println("The word you typed does not ");
    out.println("have the same characters as ");
    out.println("the password. You cannot ");
    out.println("use our precious System");
    }


    }

    }

    [/code]


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,406
    My Mood
    Mellow
    Thanks
    143
    Thanked 603 Times in 590 Posts

    Default Re: (if) Statement is not working, even when the condition is true. The (else) statement is working though.

    Use the equals() method to compare Strings:

    Password.equals( "Nathan" )

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: (if) Statement is not working, even when the condition is true. The (else) statement is working though.

    Thanks GregBrannon.

    Much appreciated.

Similar Threads

  1. IF statement accumulataion not working properly - random numbers
    By StrugglerWithJava in forum Loops & Control Statements
    Replies: 1
    Last Post: April 4th, 2013, 07:48 AM
  2. [SOLVED] "If" statement not working
    By Kanyon in forum Loops & Control Statements
    Replies: 13
    Last Post: September 15th, 2011, 01:54 PM
  3. using if statement on string System.getProperty("os.name") not working
    By sauyon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 29th, 2011, 12:09 PM
  4. switch statement not working for JFrame Calculator
    By kaddyshack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 10:35 AM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM