Results 1 to 9 of 9
- 05-19-2010, 04:01 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Password Authentication - No database
hey guyz , ive been trying to write an applet which gets the password from user , compares it with the library , if it matches , prints access granted . however i got this problem in if statement , i dont know how to compare my array which stores the password with the textfield " where user enter the password " take a look at below code , help me if u can . tnx
Java Code:/* password authentication*/ import java.awt.*; import java.applet.*; import java.awt.event.*; public class Password extends Applet implements ActionListener { private Label enterpass; private TextField password; public void init() { int pass[]={1,2,3,4,5,6}; password=new TextField(20); enterpass=new Label("Please enter your password and press enter"); add(enterpass); add(password); password.addActionListener(this); } public void actionPerformed(ActionEvent e){ if (e.getSource()==password){ /* Compare the entered values with library , if match , then show access authenticated*/ if(password==pass){ password.setText("Access Granted"); } else password.setText("Access Denied"); } } }
- 05-19-2010, 04:21 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You have declared your pass array inside the init method. That means it is only accessible inside that init method and you cannot refer to it inside the actionPerformed method. Those are basic scoping rules.
Declare it outside any method if you want it to be accessible in all the methods in your class. Also, why are you declaring the password as an int[] when the user is going to enter a String? Why not declare the pass variable as a String?
- 05-19-2010, 04:52 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
at the moment im just testing it with numbers thats why im using int , so where should i put the array then ? under the ActionPerformed module?
- 05-20-2010, 06:06 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 05-20-2010, 07:20 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
- 05-20-2010, 07:28 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Java Code:class A { //this is declared inside the class but outside any method. //it is available in all the non-static methods of this class private String pass = "12345"; public void aMethod() { //this is declared inside the method aMethod //it is only accessible inside this method String input = "123"; } }
- 05-20-2010, 07:58 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
alright , based on what u said , i did below code :
however , when i enter a , its says access denied ! whats wrong with my if statement?
Java Code:/* password authentication*/ import java.awt.*; import java.applet.*; import java.awt.event.*; public class Password extends Applet implements ActionListener { private Label enterpass; private TextField password; private String pass="a"; public void init() { password=new TextField(20); enterpass=new Label("Please enter your password and press enter"); add(enterpass); add(password); password.addActionListener(this); } public void actionPerformed(ActionEvent e){ if (e.getSource()==password){ /* Compare the entered values with library , if match , then show access authenticated*/ if(password.equals(pass)){ password.setText("Access Granted"); } else password.setText("Access Denied"); } } }
- 05-20-2010, 08:04 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
if(password.equals(pass)) is not correct. You are comparing a TextField with a String.
Instead you should be comparing the text in your textfield with the String with
Java Code:if(password.getText().equals(pass))
- 05-20-2010, 08:16 AM #9
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Similar Threads
-
jsp authentication
By tascoa in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-13-2009, 05:47 PM -
IIS Authentication
By akkarin in forum Java AppletsReplies: 0Last Post: 03-30-2009, 11:35 AM -
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum New To JavaReplies: 2Last Post: 11-14-2008, 07:53 PM -
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 0Last Post: 11-14-2008, 01:22 PM -
How to check password of a jsp/html with the password of Database(mysql) #1
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 2Last Post: 11-14-2008, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks