7 Replies Latest reply: Mar 15, 2013 10:37 AM by gimbal2 RSS

    Password With Java(Error)

    997174
      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");
                }
           }

      }
        • 1. Re: Password With Java(Error)
          Tolls
          if (passwordArray==passwordArray2))
          You need to use Arrays.equals() for that.
          What you have there is simply checking if the references are the same (ie they are the same array).
          • 2. Re: Password With Java(Error)
            997174
            can u please make the code correct for me?
            • 3. Re: Password With Java(Error)
              gimbal2
              I wonder why you want to use a character array in stead of simply using two String objects?

              And by the way: stick to the 'New To Java' forum for now. It is specifically there for people who are learning.
              • 4. Re: Password With Java(Error)
                997174
                Same problem

                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.equals(passwordArray2))

                          
                          
                     {
                               console.printf("Match\n");
                          }

                          else
                          {
                               console.printf("not Match");
                          }
                     }

                }
                • 5. Re: Password With Java(Error)
                  997174
                  i think

                  in

                  import java.io.Console;

                  does not support String arry... because if i make change

                  String[] passwordArray={'s'};
                  String[] passwordArray2 = console.readPassword("Enter your secret password: ");

                  its showing error that .readPassword not found
                  • 6. Re: Password With Java(Error)
                    gimbal2
                    You can't just try stuff and hope it works. You have to know how it works and then try stuff. I hope Tolls has more patience for you, I am not going to help someone that has not taken the time to learn all the basics first. You have a few more weeks of studying ahead of you before you're even near ready to start asking questions in forums.

                    I suggest that you read Tolls' reply to you again; you did not do what he told you to do.
                    • 7. Re: Password With Java(Error)
                      997174
                      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);
                      }
                      }
                      }

                      now ok....

                      it was wrong in my method for char

                      toCharArray() and

                      i need to use if (Arrays.equals(p1, p2)) for char arry ... tnx :p