Hello, I have created a program that is supposed to create a registration account for World of Warcraft. My main problem is when executing the program, it asks for the password TWICE mainly I believe because of this section of the code:
public static void register(Scanner input) { String testUN; String testPW; { System.out.println("Enter your username"); testUN = input.nextLine(); if (WOWAccount.okUsername(testUN) == false) { WOWAccount.addToFile(testUN); System.out.println("Username available"); } else { System.out.println("Username is not available"); register(input); } }
When I enter a username that is listed on the users.txt file, it will kick me back and ask me for a username again after displaying a message saying, "username is not available". However, If I enter a username that is not taken, everything is fine and it will prompt for a password. After I enter the password, it will complete the registration, but then ask me for a password again.
I believe the reason why that is, is because I have another code asking for the password shorty after the previous code. So my issue is that I would like to ask for the username again in the previous code and not call upon the whole register method which includes asking for the password also.
This whole error ONLY occurs if you enter a username that is in use first. If you enter a username that is not in use, it will complete the program successfully!
Here is the section of code that follows that I think contributes to the error by asking again for a password after the register method gets called due to entering a username that is not available.
do { System.out.println("Enter a Password:"); testPW = input.next(); }while(WOWAccount.okPassword(testPW) == false);
I am wondering how I can set up the program so that it doesn't ask for the password twice. I apologize if anything I stated is unclear and please ask me to clarify if that is the case. The program compiles fine and I have attached the driver along with the class.
Driver program:
import java.util.Scanner; public class TestWOWAccount { /** *Main Method */ public static void main(String[] args) { Scanner input = new Scanner(System.in); do { helloMenu(); String choice = input.nextLine().toUpperCase(); if(choice.equals("REGISTER")) { register(input); } else if(choice.equals("LOG ON")) { log_on(input); } else { System.out.println("An un-recognized command has been received." + "\nPress Enter to Continue:"); } }while(true); } /** * HelloMenu */ public static void helloMenu() { System.out.println("Please type in your selection:\n" + "Register Or Log on"); } static String filename = "users.txt"; /** * register method */ public static void register(Scanner input) { String testUN; String testPW; { System.out.println("Enter your username"); testUN = input.nextLine(); if (WOWAccount.okUsername(testUN) == false) { WOWAccount.addToFile(testUN); System.out.println("Username available"); } else { System.out.println("Username is not available"); register(input); } } do { System.out.println("Enter a Password:"); testPW = input.next(); }while(WOWAccount.okPassword(testPW) == false); input.nextLine(); // create an instance of the WOWAccount class // that passes data entered as arguments to constructor // WOWAccount user1 = new WOWAccount(testUN, testPW); // gets data from user1 and display it back System.out.println(); System.out.println("Thank you, you have successfully created " + "a new account"); System.out.println("Your username: "+ user1.getUsrName()); System.out.println("Your password: "+ user1.getPassword()); } /** * calls the Log On Method */ public static void log_on(Scanner input) { System.out.println("Log On Method has been called."); } }
Class:
import java.util.Scanner; import java.io.*; public class WOWAccount { private String username; private String password; public WOWAccount() { username = "DEFAULT"; password = "DEFAULT"; } /** *Constructor */ public WOWAccount(String testUN, String testPW) { username = testUN; password = testPW; } /** *setUsrName methods accepts an argument for the username */ public void setUsrName (String testUN) { username = testUN; } /** *setPassword methods accepts an argument for the password */ public void setPassword (String testPW) { password = testPW; } /** *getUsrName method returns the username */ public String getUsrName() { return username; } /** *getPassword method returns the password */ public String getPassword() { return password; } /** *Checks password requirement for proper validation */ public static boolean okPassword(String testPW) { int len = testPW.length(); boolean PassOK = true; int upC, countU = 0; int lowC, countL = 0; int digC, countD = 0; if (len >= 6) { for (len = 0; len < testPW.length(); len++) { if (Character.isUpperCase(testPW.charAt(len))) { countU++; } else if (Character.isLowerCase(testPW.charAt(len))) { countL++; } else if (Character.isDigit(testPW.charAt(len))) { countD++; } } if ((countU > 0) && (countL > 0) && (countD > 0)) { PassOK = true; } else PassOK = false; } else PassOK = false; return PassOK; } static String filename = "users.txt"; /** *validates username is not in use */ public static boolean okUsername(String testUN) { Scanner input = null; try { input = new Scanner(new File(filename)); while(input.hasNext()) { String curUsername = input.next(); if (testUN.equals(curUsername)) return true; } return false; } catch(FileNotFoundException e) { System.out.println("file does not exist"); } return false; } public static void addToFile(String testUN) { PrintWriter output = null; try { FileWriter fwriter = new FileWriter(filename, true); output = new PrintWriter(fwriter); output.println(testUN); } catch(IOException e) { System.out.println("ERROR!"); } finally { if (output != null) output.close(); } } }
Thank you