Comp Sci How Should I Implement the UserID and Password Generation in Java?

AI Thread Summary
The discussion revolves around implementing a UserID class in Java for generating and storing user credentials. The class should include methods for creating user IDs and passwords, with specific requirements for initialization and formatting. The user encountered a NullPointerException in the getName method due to uninitialized first and last name variables. The solution involves ensuring these variables are properly set in the constructors. Overall, the focus is on correcting the initialization process to avoid runtime errors.
csgirl504
Messages
18
Reaction score
0

Homework Statement



This is my assignment: (Sorry, it's a little long)

You are required to write a UserID Java class to be used when generating and storing the user id and password for a new user of a system. At a minimum, each UserID object should store a first name, last name, user id, and password for a user. Your class should have no public instance variables and should provide the following public methods:

UserID
(String first, String last)
Constructor to create a new UserID object, initialize the first and last
names, and generate a user id and a password, using the rules specified in
programming assignment 2.

______________________________________________________________

UserID ( ) Constructor to create a new anonymous UserID object. The first name
should be initialized to “User” and the last name to “Anonymous”. The user
id and password should be generated as specified in programming
assignment 2, except that the number which is part of the user id should be
a random integer in the range 10-99 (instead of the sum of the lengths of
the first and last names).

_______________________________________________________________

getID ( ) Accessor that returns the user id as a String object. The same user id
should be returned each time this method is invoked for an object.

_______________________________________________________________

getName ( ) Accessor to return a single String containing the name with format “LAST,
FIRST” (all uppercase) _______________________________________________________________

getPassword ( ) Accessor to return (but not modify) the currently stored password.
_______________________________________________________________

setPassword
(String newPassword)
Void method to modify the stored password with the String passed as
argument. _______________________________________________________________generatePassword ( ) Void method that generates and stores a new random password that
follows the same format as the original password.

_______________________________________________________________

toString ( ) Returns a String containing the user’s last name, first name, user id, and
password.

Homework Equations


The Attempt at a Solution



This is my program so far. I know that there is something wrong in the first two UserID methods. I was initially getting the program to work, but that was only when I initialized the private String first = ""; But I shouldn't have to do that, right? We just learned about classes, and I'm trying really hard to understand. Any help/explanation is appreciated!
Code:
import java.util.*;

public class UserID
   {
		private String userID;
		private String password;
		private String first;
      private String last;
		int num;
		Random generator = new Random();
		Scanner scan = new Scanner (System.in);
      
      public UserID(String first, String last)
      {
			String firstName = first;
			String lastName = last;
		
			// For user ID		
			String lastFive;								// First 5 letters of last name				
			String firstLetter;							// First letter of first name
			int totalCharacters;							// Total number of characters in first and last
	
		
			// For password
			Random generator = new Random();			// Generates random digits
			int num1, num2, num3, num4, num5;		// 5 random digits
			String secondLetter;							// Second letter of last name					
	

		
			// GENERATES USER ID
		
				// Gets first five letters of last name and changes letters to lowercase
				lastFive = lastName.substring(0, 5);
				lastFive = lastFive.toLowerCase();
			
				// Gets first letter of first name and changes it to lowercase
				firstLetter = firstName.substring(0,1);
				firstLetter = firstLetter.toLowerCase();
		
				// Gets number of characters in the first and last name
				totalCharacters = firstName.length() + lastName.length();
		
				// Creates user ID
				userID = lastFive + firstLetter + totalCharacters;
			
			
			
			// GENERATES PASSWORD	
		
				// Gets first letter of first name and changes it to uppercase
				firstLetter = firstLetter.toUpperCase();
			
				// Generates 5 random digits
				num1 = generator.nextInt(10);
				num2 = generator.nextInt(10);
				num3 = generator.nextInt(10);
				num4 = generator.nextInt(10);
				num5 = generator.nextInt(10);
			
				// Gets second letter of last name and changes it to uppercase
				secondLetter = lastName.substring(1, 2);
				secondLetter = secondLetter.toUpperCase();
			
				// Creates password
				password = firstLetter + num1 + num2 + num3 + num4 + num5 + secondLetter;					
			}
				
			public UserID()
			{				
				first = "User";
				last = "Anonymous";
					
				// For user ID		
				String lastFive;								// First 5 letters of last name				
				String firstLetter;							// First letter of first name
				int random;										// Random number between 10-99
	
		
				// For password
				Random generator = new Random();			// Generates random digits
				int num1, num2, num3, num4, num5;		// 5 random digits
				String secondLetter;							// Second letter of last name					
	

		
				// GENERATES USER ID
				
					// Gets first five letters of last name and changes letters to lowercase
					lastFive = last.substring(0, 5);
					lastFive = lastFive.toLowerCase();
			
					// Gets first letter of first name and changes it to lowercase
					firstLetter = first.substring(0,1);
					firstLetter = firstLetter.toLowerCase();
		
					// Generates random number
					random = generator.nextInt(90) + 10;
		
					// Creates user ID
					userID = lastFive + firstLetter + random;
			
			
			
				// GENERATES PASSWORD	
		
					// Gets first letter of first name and changes it to uppercase
					firstLetter = firstLetter.toUpperCase();
			
					// Generates 5 random digits
					num1 = generator.nextInt(10);
					num2 = generator.nextInt(10);
					num3 = generator.nextInt(10);
					num4 = generator.nextInt(10);
					num5 = generator.nextInt(10);
			
					// Gets second letter of last name and changes it to uppercase
					secondLetter = last.substring(1, 2);
					secondLetter = secondLetter.toUpperCase();
			
					// Creates password
					password = firstLetter + num1 + num2 + num3 + num4 + num5 + secondLetter;
			}
				
			public String getID()
				{
					return userID;
				}
				
			public String getName()
				{
					return (first.toUpperCase() + "," + last.toUpperCase());
				}
				
			public String getPassword()
				{
					return password;
				}
				
			public void setPassword (String newPassword)
				{
					password = newPassword;
				}
				
			public void generatePassword()
				{
					password = first.substring(0,1) + num + last.substring(1,2);
				}
				
			public String toString()
				{
					return ("Last Name: " + last + " First Name: " + first + " User ID: " + userID + " Password: " + password);
				}
		}
And this is the program I am supposed to use to test it:
Code:
import java.util.Scanner;
	
	/**
    * This program can be used to test the UserID class methods.
  	 *
	 * @author Dr.C.
    */
   public class UserIDTest
   {

      public static void main(String[] args)
      {
      	
         UserID myInfo = new UserID("John", "Lennon");
			System.out.println(myInfo.getID());
			System.out.println(myInfo.getName());
			System.out.println(myInfo.getPassword());
         System.out.println(myInfo);
      	
         UserID unknown = new UserID();
			System.out.println(unknown.getID());
			System.out.println(unknown.getName());
			System.out.println(unknown.getPassword());
         System.out.println(unknown);
			
			myInfo.setPassword("Imagine!");
			System.out.println(myInfo.getPassword());
         System.out.println(myInfo);
			
			unknown.generatePassword();
			System.out.println(unknown.getPassword());
         System.out.println(unknown);     	
      }
   
   }
I'm getting an error on the getName method.

Exception in thread "main" java.lang.NullPointerException
at UserID.getName(UserID.java:132)
at UserIDTest.main(UserIDTest.java:16
 
Physics news on Phys.org
Thats because first and last both point to null in your program. You need to make sure they are set in the constructor.
 

Similar threads

Replies
12
Views
2K
Replies
5
Views
3K
Replies
7
Views
3K
Replies
7
Views
3K
Replies
2
Views
4K
Replies
1
Views
2K
Replies
12
Views
3K
Back
Top