Question: JAVA This method helps me to save username and password in in text file in following format which I want separated by comma. James, hahjdjdks
JAVA This method helps me to save username and password in in text file in following format which I want separated by comma. James, hahjdjdks Steven, ridinsak But when I enter James for next sign up its letting user to save data instead of saying username already exits. I could not debug this code. Can someone help me ASAP Thank you public static void add_user_to_database() throws IOException { Scanner user_input = new Scanner(System.in); String user_Id, user_password; Scanner keyboard = new Scanner(System.in); File file = new File("user.txt"); FileWriter fw = new FileWriter(file, true); Scanner usernameCheck = new Scanner(file); String user = ""; while (user.equals("")) { System.out.println("Please enter a User name (q to exit): "); user = keyboard.nextLine().trim(); if (user.toLowerCase().equals("q")) { System.out.println("User Exit!"); return; // exit the method. } while (usernameCheck.hasNext()) { String existingUsername = usernameCheck.nextLine().trim(); if (user.equalsIgnoreCase(existingUsername)) { System.err.println("Username already exists! Try Again. "); user = ""; break; } } } usernameCheck.close(); // Close the Scanner file object String pass = ""; while (pass.equals("")) { System.out.println("Please enter a password for " + user + ": "); pass = keyboard.nextLine(); if (pass.equals("")) { System.err.println("Invalid Password! Try Again. "); } } fw.write(user + ","); fw.write(pass + " "); fw.close(); // close the FileWriter object. System.out.println("Account created!"); }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
