Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This programing focuses on reading/writing file, on using array structures and s

ID: 3571544 • Letter: T

Question

This programing focuses on reading/writing file, on using array structures and string class. Be sure to include

Write a Java program to create username. The program must satisfy the following requirements:

1. All the usernames must be stored into text file (user.txt)

2. You have to use array structure in your program.

3. Username isn't case sensitive.

4. Username requires at least 6 characters

5. You can't create a username starting with a number.

6. You can't create a username starting with question mark `?`

7. You can’t create a username already in use.

Before you create a username, you should read all the usernames from the text file (user.txt), and store the username into an array. After this step, you should display all the usernames from the array (not directly from the text file).

In the case of failure to meet the requirement 4, 5, 6, and 7, you should display an appropriate warning message for each case and ask user to input a new ID. If a new username satisfied all the requirements, the username should be saved into the text file (user.txt). After saving the username, you have to read the text file, store the username into an array, and display a list of usernames once again. You are to introduce at least three methods other than main: for reading a file, for writing a file and for checking “inUse”.

When you save a new user into the text file, you must keep all the previous username in the file.

user.txt;

abcdefghIJ123

Iloveyou

Explanation / Answer

Please follow the code and comments for description :

CODE :

import java.io.BufferedReader; // required imports
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class userNameValidator { // class to run the code

    private static List<String> temps = new ArrayList<>(); // instance variables

    public static void main(String[] args) { // driver method

        Scanner keyboard = new Scanner(System.in); // scanner class to get the data
        System.out.println("Initial Data : "); // message
        String name = null;
        readFromFile(); // call the function to read the data from the file
        while (true) { // iterate till the user enters a valid data
            boolean correct = false; // local variables
            boolean re = false;
            System.out.print("Enter the User Name : "); // prompt
            name = keyboard.nextLine();
            if (name.length() < 6) { // check for the length
                System.out.println("Sorry, the User name entered is less than the required length..!"); // message
                System.out.println("Please re-enter..");
                correct = false;
            } else if (name.startsWith("?")) { // check for the starting letter
                System.out.println("Sorry, the User name Entered Starts with an Illegal Character..!");
                System.out.println("Please re-enter.."); // message
                correct = false;
            } else {
                boolean flag = false;
                try {
                    int a = Integer.parseInt(name.substring(0, 1)); // check if the first letter is an integer
                    flag = true;
                } catch (Exception ex) { // if so return to re enter
                    flag = false;
                }
                if (flag == true) {
                    System.out.println("Sorry, the User name Entered Starts with an Integer..!"); // check for the flag values
                    System.out.println("Please re-enter.."); // message
                    correct = false;
                } else {
                    correct = true; // else set to true
                    boolean isExists = inUse(name); // call the method to check if the name already exists

                    if (isExists == true) { // if true the ask to re enter
                        System.out.println("Sorry, the User name Entered is already in the list..!");
                        System.out.println("Please re-enter..");
                        re = false;
                    } else {
                        re = true; // else set to true
                    }
                }
            }

            if ((correct == false && re == false) || (correct == false && re == true) || (correct == true && re == false)) { // check if the both flags are true or not
                continue;
            } else if ((correct == true && re == true)) {
                writeToFile(name); // if true then call the method to write to the file
                break; // break the loop
            }
        }
    }

    public static void readFromFile() { // methos to read the data
        BufferedReader br = null; // local variables
        FileReader fr = null;
        try {
            fr = new FileReader("user.txt"); // file reader class to read the data
            br = new BufferedReader(fr);
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) { // iterate over the data
                temps.add(sCurrentLine);
            }
            for (String s : temps) {
                System.out.println(s); // print to console
            }
        } catch (IOException e) { // catch any exceptions
            e.printStackTrace(); // print the data to console for exceptions
        } finally {
            try {
                if (br != null) { // close the reader objects
                    br.close();
                }
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace(); // print the data to console for exceptions
            }
        }
    }

    public static void writeToFile(String name) { // method to write to a file in the name

        BufferedWriter bw = null; // local variables
        FileWriter fw = null;
        PrintWriter pw = null;
        temps.add(name);

        try {
            fw = new FileWriter("user.txt"); // objects initialisations
            bw = new BufferedWriter(fw);
            pw = new PrintWriter(bw);
            for (String s : temps) {
                pw.println(s); // write the data to file
            }
            System.out.println("The Updated List of User Names are : "); // re print the data
            readFromFile(); // method call to read the data
        } catch (IOException e) { // catch any exceptions
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) { // close all objects
                    bw.close();
                }
                if (fw != null) {
                    fw.close();
                }
                if (pw != null) {
                    pw.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static boolean inUse(String name) { // check if the name already exists

        boolean found = false;
        BufferedReader br = null; // local variables
        FileReader fr = null;
        try {
            fr = new FileReader("user.txt"); // file reader class to read the data
            br = new BufferedReader(fr);
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) { // iterate over the data
                if (sCurrentLine.equalsIgnoreCase(name)) {
                    found = true; // iterate to find the value in the file if found return the boolean value
                }
            }
        } catch (IOException e) { // catch any exceptions
            e.printStackTrace(); // print the data to console for exceptions
        } finally {
            try {
                if (br != null) { // close the reader objects
                    br.close();
                }
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace(); // print the data to console for exceptions
            }
        }

        if (found == true) {
            return true; // if found return true
        } else {
            return false; // else false
        }
    }
}

user.txt :

abcdefghIJ123
Iloveyou
johnmiller124


OUTPUT :

Initial Data :
abcdefghIJ123
Iloveyou
Enter the User Name : iloveyou
Sorry, the User name Entered is already in the list..!
Please re-enter..
Enter the User Name : 1johncart
Sorry, the User name Entered Starts with an Integer..!
Please re-enter..
Enter the User Name : ?marytie
Sorry, the User name Entered Starts with an Illegal Character..!
Please re-enter..
Enter the User Name : johnmiller124
The Updated List of User Names are :
abcdefghIJ123
Iloveyou
johnmiller124


Hope this is helpful.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote