So I am having some issues opening, reading, going through, and closing a file in my code. For the project we are asked to create a program that will ask a user to either log on or register. When my user enters register, I can't get my password coding to work. Everything compiles correctly, and runs, but it doesn't do what i want it to. Its supposed to have between 6 and 22 characters where it has at least one uppercase, one lowercase and one number.
Then, when the user chooses to log on, I can't get my program to properly read through the file that I have created...it comes up with the message "Exception in thread "main" java.lang.NullPointerException" in three different spots.
here is my code:
/** * WoWDriver.java * driver for WoWAccount * @author LJones * @version 3/16/2011 v.1 * reference: Corrine Chancellor */ import java.util.Scanner; import java.io.*; public class WoWDriver { static String filename = "users.txt"; 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:"); } } } public static void helloMenu() { System.out.println("Please type in your selection:\n" + "Register Or Log on"); } public static void register(Scanner input) { String username = null; do { System.out.println("Enter a Username:"); username = input.next(); }while( WoWAccount.okUserName(username) == false); WoWAccount.okUserName(username); //look for a method that excepts okUserName //it will return a boolean String password = null; do { System.out.println("Enter a Password:"); password = input.next(); }while ( WoWAccount.okPassword(password) == false); input.nextLine(); //to consume the extra new line character; WoWAccount account = new WoWAccount(username, password); System.out.println("Congratulations! You have successfully created an account.\n" + "Username: " + account.getUsrName()+ "\nPassword: " + account.getPassword()); // saving the user's password and username to file "users.txt" PrintWriter output = null; //True = append to file, false = erase existing given data try { FileWriter fWriter = new FileWriter(filename, true); output = new PrintWriter(fWriter); output.println(username + " " + password); } catch(IOException e) { System.out.println("ERROR"); } finally { if (output !=null) output.close(); } } public static void log_on(Scanner input) { String username = null; String password = null; do { System.out.println("Enter your username:"); username = input.next(); System.out.println("Enter your password:"); password = input.next(); }while( WoWAccount.log_on(username, password) == true); WoWAccount.log_on(username, password); System.out.println("Thank you for visiting us again. Enjoy your time here!"); } }
and here is the other file
/** * WoWAccount.java * main for WoWAccount * @author LJones * @version 3/16/2011 v.1 * reference: Corrine Chancellor */ import java.util.Scanner; import java.io.*; public class WoWAccount { private String username; private String password; static String filename = "users.txt"; public WoWAccount(String usrName, String pwd) { username = usrName; password = pwd; } public void setUsrName(String usrName) { username = usrName; } public void setPassword(String pwd) { password = pwd; } public String getUsrName() { return username; } public String getPassword() { return password; } public static boolean okUserName(String userNameEntered) { Scanner input = null; try { input = new Scanner(new File(filename)); while(input.hasNext()) { String curUserName = input.next(); // String curPswd = input.nextLine(); if(userNameEntered.equals(curUserName)) return false; } return true; } catch(FileNotFoundException e) { System.out.println("The file does not exist"); } return false; } public static boolean okPassword(String passwordEntered) { //identifying variables int countU = 0; int countL = 0; int countD = 0; int i; { int passwordEnteredLength = passwordEntered.length(); //begin if-else for password length if ((passwordEnteredLength > 5) && (passwordEnteredLength < 21)) { for(i = 0; i < passwordEnteredLength; i++) { if(Character.isUpperCase(passwordEntered.charAt(i))) { countU = countU++; } } //end "for" for uppercase count for(i = 0; i < passwordEnteredLength; i++) { if(Character.isLowerCase(passwordEntered.charAt(i))) { countL = countL++; } } //end "for" for lowercase count for(i = 0; i < passwordEnteredLength; i++) { if(Character.isDigit(passwordEntered.charAt(i))) { countD = countD++; } } //end "for" for digit count //determining if the password meets requirements if ((countU > 0) && (countL > 0) && (countD > 0)) return false; else { System.out.println("Must have at least one uppercase, one lowercase and one number included. \n" + "Please try again."); } return true; } //end "if" part of password length if-else else { System.out.println("Your password must be between 6 and 20 characters"); return false; } } } //end okPassword string method public static boolean log_on (String enteredUserName, String enteredPassword) { Scanner input = null; try { File file = new File(filename); Scanner readFile = new Scanner(file); while(readFile.hasNext()) { String username = input.next(); String password = input.nextLine(); if((enteredUserName.equals(username)) && (enteredPassword.contains(password))) return true; } return false; } catch (FileNotFoundException e) { System.out.println("The file does not exist"); } finally { if (input != null) input.close(); } return false; } }
Thank you for taking your time to look over my coding!! You're helping a very new to programming individual!!