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: Simple password program (If statements not working)

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple password program (If statements not working)

    Hello all. I am just practicing my Java and decided to make a super simple and insecure password program. This program has a hardcoded string that is the password, and gets user input as a string and compares the two. If they match its supposed to change a boolean (that determines if the user is currently logged in) from false to true; and if the user gets it wrong its supposed to output the string "bad password" and the value of the boolean as false.

    The problem is no matter what is entered the wrong password string is displayed, even it the strings match. I have no idea why this is, everything compiles and runs, so no error messages. So perhaps im missing something obvious. Thanks in advanced for any help.

    Code:
    import java.util.Scanner;
     
     
    public class LogIn {
        public static void main(String[] args) {
            boolean userLoggedIn = false;
            boolean mainLoop = true;
            String passwd = new String("abc123");   //The password that must be entered
            Scanner inputPass;
     
            while(mainLoop) {
                inputPass = new Scanner(System.in);
                System.out.print("Enter password>");
                String inputPassString = inputPass.next();    //Convert Scanner to String.
     
                if(inputPassString == passwd) {
                    userLoggedIn = true;    //User is now logged in.
                    System.out.println("Logged in: " + userLoggedIn);
                    mainLoop = false;
                } else {
                    System.out.println("User input: " + inputPassString);    //These two prove that strings match.
                    System.out.println("User passwd: " + passwd);
                    System.out.println("Bad pass. Logged in: " + userLoggedIn); //User should not be logged in.
                    mainLoop = true;
                }
            }
        }
    }
    Last edited by AwesomePilot; March 21st, 2018 at 01:23 AM. Reason: Typo

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,406
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Simple password program (If statements not working)

    Use the equals() method to compare two String objects, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple password program (If statements not working)

    Oh well that was a simple solution. Thanks a lot, it works beautifully now.

Similar Threads

  1. Need help with programming a simple password checker.
    By HappyCamper in forum What's Wrong With My Code?
    Replies: 22
    Last Post: September 1st, 2014, 12:32 PM
  2. Simple Address Book Program Not Working....
    By programmer101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2013, 01:21 AM
  3. Simple Calcular Program, But not working!!!
    By achugr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2012, 02:53 PM
  4. If statements not working as planned
    By davidvee in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2012, 03:28 PM
  5. Simple beginner's password guessing program
    By edishuman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 12th, 2011, 03:59 PM