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 5 of 5

Thread: java code for password

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java code for password

    Hi, this is the java code that I wrote for a password:

    // java application that asks the user to enter a password
     
    import java.util.Scanner; //program uses class scanner
     
    public class password{
     
    	//main method used to execute java application
     
    	public static void main(String args[]){
     
    		//create scanner to obtain input from command window
     
    		Scanner user_input=new Scanner (System.in);
     
     
    		String pass="reason"; //string that is declared for the password
     
    		System.out.print("entered the password"); //prompt
            pass=user_input.next(); //input from user
    // outputs two different statements if the user either inputs the password or something else
            if(pass=="reason")
              System.out.print("You entered the correct password");
            else{
              System.out.print("Try again");}
     
     
     
     
     
    	} //end method
     
    } //end class password

    here is the output of the code when I run it :

    entered the passwordreason
    Try againPress any key to continue . . .
    The code compiles without errors. The problem is when , I entered the actually password that I declared in my code, it doesn't output "You entered the correct password" but it instead outputs "Try again", which is supposed to be the output if you've entered the incorrect password. What did I do wrong?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,109
    My Mood
    Mellow
    Thanks
    186
    Thanked 656 Times in 643 Posts

    Default Re: java code for password

    if(pass=="reason")

    Do not use the operator '==' to compare Strings. Use the equals() method.

    Note: Next to NPEs that have varied causes, the error above is the most common seen in a forum like this.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java code for password

    so write "if(pass()"reason")

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,109
    My Mood
    Mellow
    Thanks
    186
    Thanked 656 Times in 643 Posts

    Default Re: java code for password

    equals() is a method applied to objects:

    if ( pass.equals( "reason" ) )

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java code for password

    Thanks that worked

Similar Threads

  1. Password With Java(Error)
    By sarwardnj in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 15th, 2013, 10:55 AM
  2. A password code
    By obiedog in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 29th, 2011, 12:25 PM
  3. password.java
    By nickpuma19 in forum Object Oriented Programming
    Replies: 5
    Last Post: November 11th, 2010, 12:29 AM
  4. [SOLVED] java password
    By pwngrammer in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 15th, 2009, 09:49 AM
  5. Cracking password in less number of Trials using brute forcea
    By xisstar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 10th, 2009, 10:40 AM