Results 1 to 4 of 4
  1. #1
    fable13 is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default Compiles and runs but won't recognize password

    Hello I am new to java and I am making a little program to test some knowledge. It compiles and runs but does not compare the passwords correctly. when I type in "blue" for the password it says that it is incorrect. Why is this? Any help would be much appreciated.

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

    class Withdraw {

    public static void main(String args[]) {

    int balance = 1058;
    int newBalance = 0;

    //Password scanner
    Scanner passScanner = new Scanner(in);

    //Withdraw scanner
    Scanner myScanner = new Scanner(in);

    out.println("Thank you for banking with us");
    out.print("Please enter your password ");

    String password = passScanner.next();
    out.println();

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

    //Compares entered password to string "blue"
    while (password != "blue") {
    out.println("Incorrect Password");
    out.println("Please re-enter your password");
    password = passScanner.next();
    out.println("You typed >> " + password + " <<");
    }

    //Banking portion
    out.println("Your current balance is $1,058");
    out.print("How much would you like to withdraw? ");

    int inputNumber = myScanner.nextInt();

    while (inputNumber > balance) {
    out.println("******************");
    out.println("Insufficient Funds");
    out.println("Your balance is $" + balance);
    out.println();
    out.print("Please enter a valid amount $");
    inputNumber = myScanner.nextInt();
    }

    newBalance = balance - inputNumber;
    out.println("******************");
    out.println("Your new balance is " + newBalance);
    out.println("******************");

    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    When comparing the contents of objects (String is an object) use the equals() method, not == or !=
    == is for comparing primitives or to see if two references point to the same object.

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Also for future reference, please wrap your code in [code][/code] tags so its more readable for us.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    fable13 is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    Thank you for all the help. I will make sure I use the tags when posting next time, sorry about that.

Similar Threads

  1. Replies: 6
    Last Post: 02-08-2011, 02:29 AM
  2. Code compiles in/runs in IDE but not on UNIX system
    By Unclejunebug in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 08:35 AM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  5. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 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
  •