Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: user name and password validation

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy 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:

    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
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: user name and password validation

    The array required message is a problem with your instance variables, userName and password are Strings not string arrays.
    Not sure about the second point but this should be able to help
    http://www.javaprogrammingforums.com...html#post18632

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: user name and password validation

    thanks for your input, i have corrected few things and now my code looks like:

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

    errors what i have now are:

    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
    Junior Member iCoder's Avatar
    Join Date
    Jan 2011
    Location
    127.0.0.1
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: user name and password validation

    length is not a method, use it like a property.
    username.length; not as
    username.length();

    ur method type is void, it means it doesn't RETURN any value.

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Re: user name and password validation

    ah ok, corrected:

    	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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: user name and password validation

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/37618-user-name-password-validation.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member iCoder's Avatar
    Join Date
    Jan 2011
    Location
    127.0.0.1
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: user name and password validation

    Create a object for guessing game class
    GuessingGame guess = new GuessingGame();
    guess.methodname();

  8. #8
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: user name and password validation

    Create a object for guessing game class
    GuessingGame guess = new GuessingGame();
    guess.methodname();
    yes he is write you need to create object of Guessing Game and this is because
    start method is not static and you cannot call non static methods using class name.

    Static


    and please mark this thread solved.
    Last edited by DanBrown; January 21st, 2011 at 05:37 AM.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Jtextfield Validation
    By nimishalex in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2010, 02:42 AM
  2. Code Validation help??
    By kmh90210 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 30th, 2010, 11:25 AM
  3. Exception during xml validation
    By vijeta in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 6th, 2010, 02:50 AM
  4. IPv6 validation
    By subhvi in forum Java Networking
    Replies: 1
    Last Post: November 27th, 2009, 07:19 AM
  5. Problems with If validation
    By websey in forum Loops & Control Statements
    Replies: 1
    Last Post: November 18th, 2009, 09:43 AM