DO NOT USE BUFFEREDREADER. PLEASE USE SCANNER INSTEAD!!!!! ALSO USE PRINTSTREAM
ID: 3902772 • Letter: D
Question
DO NOT USE BUFFEREDREADER. PLEASE USE SCANNER INSTEAD!!!!!
ALSO USE PRINTSTREAM IF NEEDED!
I) Many online and offline services are associated with a user identity and credential. In this assignment, you will create a rudimentary (and highly insecure) database for the storage of only the user generated identities. This program will have the following behaviors: A) Prior to prompting for a new username, the existing list of usernames should be read and loaded to an array 1) Read the list from "users.txt", a prompt is not necessary, but the filename should be displayed. 2) The existing list of usernames should be written to the console. Upon prompting, the following checks must be made against an attempt to create a new username. 1) Duplicate usernames should be disallowed, and the usernames are not case sensitive. 2) Usernames must be between 8 and 20 characters in length. 3) The username can only contain letters between "A" to "Z" OR "a" to "z". 4 ) If the rules from A-Care violated, an appropriate error message informing the user should be displayed. B) i ) The error message should display *all* the reasons why the username is not correct. ii) Do not display merely the first problem the program identifies (unless, of course, the first problem is the only problem) C) When a valid username is provided, this username should be written to an array and printed back to console. Finally, the "users.txt" database should be updated such that the new username appears should the program be run again D)Explanation / Answer
import java.io.*;
import java.util.*;
public class DemoUser{
public static void main(String[] args){
try {
String[] data = new String[100];
FileInputStream fin = new FileInputStream("users.txt");
Scanner fc = new Scanner(fin);
Scanner sc = new Scanner(System.in);
int count = 0;
while (fc.hasNextLine()){
data[count] = fc.nextLine();
System.out.println(data[count]);
count++;
}
fc.close();
while(true){
System.out.println("Create a new user:");
String line = sc.nextLine();
int valid1 = 1;
if (line.length() < 8 || line.length() > 20){
System.out.println("Error: length of input should be between 8 and 20");
valid1 = 0;
}
int valid2 = 1;
for (int i = 0; i<line.length(); i++){
if (!Character.isLetter(line.charAt(i))){
valid2 = 0;
break;
}
}
if (valid2 == 0){
System.out.println("Error:only alphabets are allowed");
}
int valid3 = 1;
for (int i = 0; i<count; i++){
if (data[i].equals(line)){
valid3 = 0;
break;
}
}
if (valid3 == 0){
System.out.println("Error:Duplicate name");
}
if (valid1 == 1 && valid2 == 1 && valid3 == 1){
try {
System.out.println("User " + """" + line + """" + " added to " + ""userArray" ");
data[count] = line;
count++;
FileOutputStream fout = new FileOutputStream("users.txt");
PrintWriter pw = new PrintWriter(fout);
System.out.println("Contents of userArray:");
for (int i = 0; i<count; i++){
System.out.println(data[i]);
pw.println(data[i]);
}
pw.close();
System.out.println(" "userArray" " + "is written succesfully to " + " "users.txt" ");
}
catch (Exception e){
e.printStackTrace();
}
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.