Results 1 to 7 of 7
  1. #1
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Question user name and password validation

    Hi,

    I have created Guessing Game where computer is generating random number from 1-100 and then user needs to guess it.

    as the second part of the assignment I need to:

    1. Restructure the application you created in exercise 5 so that it now carefully controls who has access to play the Guessing Game. Create a separate class called UserValidation which is used to check a supplied password for a given user against a stored password. If the two passwords match then the user should be allowed to proceed, otherwise the user should be asked to re‐enter both username and password. This process should be repeated without end.
    a. The UserValidation class should contain an array of user names and corresponding passwords. For simplicity assume there will be no more than 10 valid users.
    b. The password array should be populated at the point of object instantiation (i.e. in the class constructor) – using hard‐coded values, with usernames ordered alphabetically.
    c. The UserValidation class needs only one public method – called ‘checkPwd()’ which is used to compare the supplied username/password pair with those stored internally using a simple Linear search routine.
    d. The UserValidation class should have as many private methods as you deem necessary and appropriate. However, this class should NOT be used to read in the username and passwords – that should be accomplished by one of the methods within your GuessingGame class.

    What I did was, I created new class called UserValidation, here you have the code:

    Java Code:
    import java.util.*;
    import java.io.*; //Provides for system input and output through data streams, serialization and the file system. (for example Scanner class)
    
    public class UserValidation
    
    {
    	public String userNameInput;
    	public String passwordInput;
    	public String password;
    	public String userName;
    	private Scanner scan;
    
    		public static void main(String[] args) throws IOException
    		{
    	       // declares an array of Strings for user name
    
            String[] userName = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj"};
    
            String[] password = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj"};
    
            UserValidation hiLo = new UserValidation();
    			//pointing new object to new method called go()
    		hiLo.go();
    
              }
    
    		public void go()
    		{
    		scan = new Scanner(System.in) ; // Creates Scanner instance
    		System.out.print("What is your user name? :");
    
    		//input point
    		userNameInput = scan.next();
    
    		System.out.print("What is your password? :");
    
    		//input point
    		passwordInput = scan.next();
    
    
    		//loop for the decision point to start the game
    		if((userNameInput.equals(userName[0])) || (passwordInput.equals(password[0])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    		else if((userNameInput.equals(userName[1])) || (passwordInput.equals(password[1])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    		else if((userNameInput.equals(userName[2])) || (passwordInput.equals(password[2])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    		else if((userNameInput.equals(userName[3])) || (passwordInput.equals(password[3])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    		else if((userNameInput.equals(userName[4])) || (passwordInput.equals(password[4])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    		else if((userNameInput.equals(userName[5])) || (passwordInput.equals(password[5])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    		else if((userNameInput.equals(userName[6])) || (passwordInput.equals(password[6])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    		else if((userNameInput.equals(userName[7])) || (passwordInput.equals(password[7])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    		else if((userNameInput.equals(userName[8])) || (passwordInput.equals(password[8])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    		else if((userNameInput.equals(userName[9])) || (passwordInput.equals(password[9])))
    		{
    			//program is moving to new method
    			GuessingGame.start();
    
    		}
    			//negative response for a loop
    		else
    		{
    			System.out.println("your User Name or Password is wrong");
    		}
    
    
    	}
    
    
    }
    I am getting few errors,

    1. array required, but java.lang.String found in every if, else if line "if((userNameInput.equals(userName[0])) || (passwordInput.equals(password[0])))"

    2. "non-static method start() cannot be referenced from a static context" in GuessingGame.start(); I am assuming here I need to point it to an object not a method right?

    or maybe there is any other way to build this new class?
    Thanks in advance
    Exose

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    It is code, which show how not to do ).
    You can use loop for checked login and password, for example.
    Java Code:
        private final static String[] users = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj"};
        private final static String[] passwords = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj"};
        
        public boolean checkLoginPassword(String login, String password) {
            for (int i=0;i!=users.length;++i) {
                if (users[i].equals(login) && passwords[i].equals(password)) {
                    return true;
                }
            }
            return false;
        }
    On the second question you need read topic about Controlling Access to Members of a Class
    Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    thanks for your help

    I have corrected my code a little bit and now it looks like:

    Java Code:
    public class UserValidation
    
    {
    	private Scanner scan;
    	public String userNameInput;
    	public String passwordInput;
    	public String[] userName = new String[10];
    	public String[] password = new String[10];
    
    
    		public static void main(String[] args) throws IOException
    		{
    	       // declares an array of Strings for user name
    
        	String[] userName = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj"};
    
        	String[] password = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj"};
    
            UserValidation hiLo = new UserValidation();
    			//pointing new object to new method called go()
    		hiLo.ArraySearch();
    
              }
    
    		public void ArraySearch ()
    		{
    		scan = new Scanner(System.in) ; // Creates Scanner instance
    		System.out.print("What is your user name? :");
    
    		//input point
    		userNameInput = scan.next();
    
    		System.out.print("What is your password? :");
    
    		//input point
    		passwordInput = scan.next();
    
    	        		for (int i = 0; i < userName.length(); i++)
    	        			{
    	        		        if(userName.charAt(i).equals(userNameInput))
    	            			{
    	                			 System.out.print("your user name is :" + userNameInput ); // Returns the index
    	            			}
    	            			return true;
    	        			}
    
    	        		return false;
    
    	    }
    
    }
    with errors:


    1. cannot find symbol method length()
    2. cannot find symbol method charAt(int)
    3. cannot return a value from method whose result type is void for "return false"

    :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    userName is an array of String, not a String, and so the compiler is telling you correctly that those methods do not exist for it.

  5. #5
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    ah ok, corrected:

    Java Code

    Java Code:
    	for (int i = 0; i < userName.length; i++)
    	    {
    	        if(userName[i].equals(userNameInput) || password[i].equals(passwordInput))
    	        {
    	             System.out.print("Your user name and password are correct, you can play the game now"); // Returns the index
    	        }
    	        GuessingGame.start();
    	    }
    	 
    	return false;
    I want to now point this to GuessingGame class, and start from "start" method, I have used "GuessingGame.start();" but this is throwing me an error:

    ""GuessingGame.start();" non-static method start() cannot be referenced from a static context"

    should I point it to an object instead?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. 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, 07:53 PM
  2. 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, 01:22 PM
  3. 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, 01:11 PM
  4. logic to restrict the user from entering the password morethan thrice?
    By kavitha_164951 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-12-2008, 02:18 PM
  5. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 07:03 AM

Posting Permissions

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