Java generator

One of the most easiest exercises you can do using – Strings and Random – libraries from Java is creating an ID generator for you to use when creating users in your program or web application.

This is useful when instead of creating your own system, let’s say of registering users, you can give them a random generated Id for them to use every time the log in into your application. This program consist of accepting a user first, last names and a random generated number from the range of 10-99. This is explained way better in the description of the program. Feel free to play with it.

This time I had to do the Id generator as an assignment from one of my classes. Here it is:

//********************************************************************
//  Class3.1.java       Author: Kevin Uriel Azuara Fonseca
//
//  3.1 Write a program that prompts for and reads the user’s first and last name (separately).
//  Then print a string composed of the first letter of the user’s first name, followed by the first five characters of the user’s last name,
//  followed by a random number in the range 10 to 99. Assume that the last name is at least five letters long.
//  Similar algorithms are sometimes used to generate usernames for new computer accounts.
//********************************************************************
import java.util.Scanner;
import java.util.Random;

public class Class31 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        String firstName, lastName, firstLetter, fiveLetters, userId;
        int numberGenerator;
        
        Scanner input = new Scanner(System.in);
        Random randomGenerator = new Random();
        // Insert first and last name inputs
        System.out.println("Enter first name:");
        firstName = input.nextLine();
        System.out.println("Enter last name:");
        lastName = input.nextLine();
        
        // string composed by the first letter of the user first name
        firstLetter = firstName.substring(0,1);
        // first five characters of the user last name
        fiveLetters = lastName.substring(0, 5);
        // random number generator from 10-99
        numberGenerator = randomGenerator.nextInt(90) + 10;
        
        // Generate random userId
        userId = firstLetter + "" + fiveLetters + "" + numberGenerator;
        
        // Print the generated userId
        System.out.println(" userId: "+ userId +"  ");
    }
    
}

Remember to share this article with your classmates in case they get the same assignment as I did but without them copying it. Instead make sure(this goes for you too, the reader) they read it and understand it for them to create their own system.

I would like to have you to visit this article in which I explained how to easily create a login, register and recover system using the PHP framework CodeIgniter. CodeIgniter is the most easiest framework to learn but unfortunately there is not much demand for it. Here it is the article:

How to Create a Recover and Verification System using CodeIgniter

Bye-Bye 🙂

Leave a Reply

Back to Top