-9

I am using NetBeans and the code is

    private void passwordActionPerformed(java.awt.event.ActionEvent evt)     {                                         
        // TODO add your handling code here:  
    }       

How to make java scan for a certain password, so that, if it is wrong it does not work and if the password is correct it works and performs an action.

3
  • Show what else have you tried... Its not clear what you want... Paste at least minimal code that can be compiled and verified
    – CoderNeji
    Jul 27, 2015 at 11:15
  • I am asking what I have to write in that code so that it looks for password and sees if it is right
    – JohnTJC
    Aug 5, 2015 at 7:49
  • Simply on your event firing take value from a textbox or passwordbox first then do your further manipulations... Would have been better if you showed your whole code... Would have helped you out
    – CoderNeji
    Aug 5, 2015 at 8:56

3 Answers 3

0

You should have an simple text/password input field awt, read the input into a variable and simply compare it with a if.

1
  • From the question, it's fair to assume that the OP knows that they should have a password field - this answer pretty much paraphrases the question, up to the "How" part. Could you add a solid example that demonstrates how to access the password and make a simple comparison that results in an appropriate return value? It's certainly fair to say "BTW - plain text passwords are bad, 'mkay?" - not your job to solve the problem that hasn't been presented, but helpful to point it out.
    – Mogsdad
    Aug 21, 2015 at 19:35
0

Don't use NetBeans. Use Eclipse instead and code the way you want the window to appear, don't make the gui by drag-n-drop

1
  • I guess the upvotes are from NetBeans haters, because this makes no attempt to answer the question about how to validate the password. (The OP could do all you say, and will STILL have the problem that brought them here.) Maybe you could show code demonstrating how it would be done without the NB D&D GUI.
    – Mogsdad
    Aug 21, 2015 at 19:31
0

Hope it is not too late to reply for this. If so then another one who refers this may find this answer helpful.

For your particular question the answers is that you simply can use an if statement to perform this.

Assuming that your password jPasswordfield is "jpassText" and your password is "abc123"

   if(jpassText.getText().trim().equals("abc123")){
        System.out.println("Password is correct");
   }else{
        System.out.println("Please re-try");
   }

For the sake of learning this is fine. But if you move into a login screen there are several steps to be followed.

step 1 : get the username and the password

step 2 : trim them and check for empty strings

step 3 : check whether the user is available in the database (of course you should keep a db and query for the user details from it)

step 4 : encrypt the password and compare both username and the password with the table and if success grant the login else let the user try again.

When saving passwords it is better to encrypt them so that it is not saved in plain text. When querying, user entered password should be encrypted with the same algorithm it was saved to the database and compare the encrypted password string with the queried password.

This practice can be followed even when you try to give some administrative facilities inside your application for certain group of users.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.