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 :((
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");
}
}
}
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.
Re: Password With Java(Error)
can you please make the code correct for me?
Re: Password With Java(Error)
Did you find the API doc for the class I suggested? What method did you try?
Re: Password With Java(Error)
i have used
if( passwordArray.equals(passwordArray2))
still same problem :(
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
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);
}
}
}
Re: Password With Java(Error)
I'm glad you got it working.