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

Thread: Password With Java(Error)

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Password With Java(Error)

    Hi !

    I want to make a console window program which take a password from window and compare it with a predefined password and then will show password match or not match.

    my code is where which is compiling and running too.

    But the problem is that when i am taking password input(wrong or correct password in both) from console its always showing result "not match" . please help me

    here is the code
     
    import java.io.Console;
     
    public class Main
    {
    	public static void main(String[] args)
    	{
     
    		Console console = System.console();
     
        	char[] passwordArray={'s'};
     
        	char[] passwordArray2 = console.readPassword("Enter your secret password: ");
     
    		if((passwordArray==passwordArray2))
        	{
    			console.printf("Match\n");
    		}
     
    		else
    		{
    			console.printf("not Match");
    		}
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Password With Java(Error)

    The == operator does not compare the contents of two arrays.
    See the Arrays class for a method that may help.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password With Java(Error)

    can you please make the code correct for me?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Password With Java(Error)

    Did you find the API doc for the class I suggested? What method did you try?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password With Java(Error)

    i have used

    if( passwordArray.equals(passwordArray2))

    still same problem

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Password With Java(Error)

    Did you read the API doc for the Arrays class to see if any of its methods would help you?
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password With Java(Error)

    [solved]
    tnx norm ur link helps me much....

    import java.util.Arrays;
    toCharArray() and if (Arrays.equals(p1, p2)) helps me much



    import java.io.Console;
    import java.util.Arrays;

    public class Password{

    public static void main(String[] args) {
    Console console = System.console();

    char[] password = "shaon".toCharArray();

    char[] passwordEntered = console.readPassword("Enter password: ");

    if (Arrays.equals(password, passwordEntered))
    {
    System.out.println("\n Access granted \n");

    }
    else
    {
    System.out.println("Access denied");
    System.exit(1);
    }
    }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Password With Java(Error)

    I'm glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Password Saver Problem
    By chesspro13 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 18th, 2013, 06:35 PM
  2. Java Password Saver Problem
    By chesspro13 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 18th, 2013, 06:02 PM
  3. Java password encryption
    By jmorr212 in forum JDBC & Databases
    Replies: 2
    Last Post: January 29th, 2011, 02:33 AM
  4. password.java
    By nickpuma19 in forum Object Oriented Programming
    Replies: 5
    Last Post: November 11th, 2010, 01:29 AM
  5. [SOLVED] java password
    By pwngrammer in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 15th, 2009, 09:49 AM