Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started