Results 1 to 9 of 9
  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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?

  3. #3
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by pinkdreammsss View Post
    at the moment im just testing it with numbers thats why im using int ,..
    You are not using int. You are using int[].

    Quote Originally Posted by pinkdreammsss View Post
    ..so where should i put the array then ? under the ActionPerformed module?
    Read my reply above again. And don't say vague things like module. actionPerformed is a method.

  5. #5
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    You are not using int. You are using int[].


    Read my reply above again. And don't say vague things like module. actionPerformed is a method.
    i dont get what u mean !! declare it outside any method i wanna use ... could u gimme an example ?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

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

  7. #7
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

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

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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))

  9. #9
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

Similar Threads

  1. jsp authentication
    By tascoa in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-13-2009, 06:47 PM
  2. IIS Authentication
    By akkarin in forum Java Applets
    Replies: 0
    Last Post: 03-30-2009, 12:35 PM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 08:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 02:22 PM
  5. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 02:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •