This is Java programing This programing focuses on readingwr

This is Java programing.

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

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”.

user.txt;

abcdefghIJ123

Iloveyou

Solution

import java.util.*;
import java.io.*;
public class HelloWorld{
  
    //declare instance variables
    static String usernames[]=new String[100];
    static int count=0;
  
    //function to read data from file
    public static void readFromFile()
    {
        //open file for readinbg and store read data to array
        BufferedReader br = null;
       try {
           String sCurrentLine;
           br = new BufferedReader(new FileReader(\"user.txt\"));
           while ((sCurrentLine = br.readLine()) != null) {
               usernames[count]=sCurrentLine;
                               count++;
           }
           br.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
      
       //display the data in array
       System.out.println(\"Usernames in file are:\");
       for(int i=0;i<count;i++)
       System.out.println(usernames[i]);
    }
  
    //function to write data to file
    public static void writeToFile(String uname)
    {
        try {
           File file = new File(\"user.txt\");
           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();
           }
           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);
           bw.write(uname);
           bw.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
       readFromFile();
    }
  
    //function to check if username exits in file or not
    public static boolean isInUse(String uname)
    {
        for(int i=0;i<count;i++)
        {
            if(usernames[i].toLowerCase().equals(uname.toLowerCase()))
            return true;
        }
        return false;
    }
  
    //function to check if usernam eis valkid or not
    public static String isValid(String uname)
    {
        if(uname.length()<5)
        return \"Length should be greater than 6\";
        else if(uname.charAt(0)==\'?\' )
        return \"Username should not start with ?\";
        else if(Character.isDigit(uname.charAt(0)))
        return \"Username should not start with a number\";
        else if(isInUse(uname))
        return \"Username already exists\";
        else
        return \"\";
    }

//main function which calls the above functions
     public static void main(String []args){
      
       //declare required variables
       boolean flag=true;
       String uname;
      
       //read file into array
       readFromFile();
      
       //prompt user to input a new username
       Scanner read=new Scanner(System.in);
       //loop until user enters a valid username
       do
       {
            System.out.print(\"Enter user name: \");
            uname=read.next();
            String err=isValid(uname);
            //check if entered value is valid or not
            //if valid write to file
            if(err.equals(\"\"))
            {
                writeToFile(uname);
                flag=false;
            }
          
            //otherwise prompt the user to enter again
            else
            {
                System.out.println(err);
            }
       }while(flag);
     }
}

Sample Output:

U

sernames in file are:                                                                                                                                          

abcdefghIJ123                                                                                                                                                   

Iloveyou                                                                                                                                                        

Enter user name: oliveyout                                                                                                                                      

Usernames in file are:                                                                                                                                          

abcdefghIJ123                                                                                                                                                   

Iloveyou                                                                                                                                                        

oliveyout

This is Java programing. This programing focuses on reading/writing file, on using array structures and string class. Write a Java program to create username. T
This is Java programing. This programing focuses on reading/writing file, on using array structures and string class. Write a Java program to create username. T
This is Java programing. This programing focuses on reading/writing file, on using array structures and string class. Write a Java program to create username. T

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site