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:
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
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
Re: user name and password validation
thanks for your input, i have corrected few things and now my code looks like:
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;
}
}
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"
:)
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.
Re: user name and password validation
ah ok, corrected:
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?
Re: user name and password validation
Re: user name and password validation
Create a object for guessing game class
GuessingGame guess = new GuessingGame();
guess.methodname();
Re: user name and password validation
Quote:
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.