我正在编写一个基本程序,它有三个菜单选项:创建用户、登录和退出。然后,用户可以选择这些菜单选项之一。
如果他们选择Create,他们将被提示输入一个用户ID和密码(这必须遵循模式),然后将检查一个.DAT文件,以确保用户ID尚未被使用。成功完成后,程序将把新的用户ID和密码写入.DAT文件的末尾。
如果他们选择登录,他们将被提示输入他们的用户ID,然后他们的密码,然后程序将读取.DAT文件,以验证他们是记录。
选择“退出”将显示一条消息,“您已签名退出”。
作为一个预警,我在java编程方面还是个新手。
我在代码中遇到的问题:选择新用户没有附加到选择登录程序的.DAT文件中,似乎没有正确地检查.DAT文件,因为即使我输入了一个现有的帐户信息,它仍然给出了我的错误“无效的用户ID”。
import java.io.*;
import java.util.*;
/**
* This program will utilize a menu structure and validate input if the user doesn't choose
* a correct option. Writes a new ID and password to .dat file when user chooses to
* create new user from menu.
*
* @author CMS
* @Date 7/28/2014
*/
public class Passwordv2 {
static boolean answer = true;
static final String MENUANSW = "[1-3]{1}", USERID = "[A-Z,a-z]{6}-[0-9]{2}"; //, PASSWORD = "";
static String iMenuOption="", iID, recPassword, recUserID, password;
static Scanner scanner,scannerDat;
static PrintWriter pw;
public static void main(String[] args) {
init();
while (answer == true) {
menu();
if (iMenuOption.equals("1")) createUser();
else
if (iMenuOption.equals("2")) signIn();
else {answer = false;}
}
closing();
} // end of main
public static void init(){
//User input scanner
scanner = new Scanner(System.in);
//PrintWriter
try {
pw = new PrintWriter(new FileOutputStream (new File ("account.dat"),true) );
}
catch(FileNotFoundException ex) {
}
} // end of INIT
public static String menu(){
do {System.out.println("Please select from the following:");
System.out.println("1. Create a New User");
System.out.println("2. Sign in");
System.out.println("3. Exit");
iMenuOption = scanner.next();
answer = isValMenuOption(iMenuOption);
if (answer == false) { System.out.print("Incorrect Choice. ");}
} while (!answer);
return iMenuOption;
}
public static boolean isValMenuOption(String iMenuOption) {
return(iMenuOption.matches(MENUANSW));
}
public static void createUser() {
boolean validID = true, newID = true;
do {if (!validID) {System.out.println("User ID did not meet requirements.");}
if (!newID) {System.out.println("This User ID has been taken.");}
System.out.println("Please select a User Id (6 letters, followed by a dash (-), followed by 2 numbers).");
iID = scanner.next();
validID = isValidID(iID);
newID = isNewID(iID);}
while (validID==false || newID == false);
boolean valLength = true, valNum = true, valUpper = true, valLower = true;
do{ System.out.println("Please select a Password:");
System.out.println("(6-12 characters, one number, one upper case, one lower case, no white space or symbols).");
password = scanner.next();
valLength = isValLength(password);
valNum = valNum(password);
valUpper = valUpper(password);
valLower = valLower(password);}
while (!valLength || !valNum || !valUpper || !valLower);
pw.println(iID);
pw.println(password);
//menu();
}
public static boolean isValidID(String iID){
return(iID.matches(USERID));
}
public static boolean isNewID(String iID){
answer = true;
// Dat file scanner
try {
scannerDat = new Scanner(new File("account.dat"));
scannerDat.useDelimiter("\r\n");
}
catch (Exception e) {
e.printStackTrace();
System.out.println("File error");
System.exit(1);
}
while (scannerDat.hasNext()) {
recUserID = scannerDat.next();
recPassword = scannerDat.next();
if (recUserID.equals(iID)) {
answer = false;
break;
}
}
return answer;
}
public static boolean isValLength(String password) {
if (password.length() <6 || password.length() > 12) System.out.println("Password did not meet length requirements. ");
return(password.length() >= 6 && password.length() <= 12);
}
public static boolean valNum(String password) {
if (password.matches(".*[0-9].*") == false) System.out.println("Password must contain at least one number. ");
return(password.matches(".*[0-9].*"));
}
public static boolean valUpper(String password){
if (password.matches(".*[A-Z].*") == false) System.out.println("Password must contain one upper case letter.");
return (password.matches(".*[A-Z].*"));
}
public static boolean valLower(String password){
if (password.matches(".*[a-z].*") == false) System.out.println("Password must contain one lower case letter.");
return (password.matches(".*[a-z].*"));
}
public static void signIn() {
boolean newID;
System.out.println("Enter User ID.");
iID = scanner.next();
System.out.println("Enter Password.");
password = scanner.next();
newID = isNewID(iID);
if (newID == false) {
if (password.equals(recPassword)) {System.out.println("Authenticated. You have signed in.");}
else {System.out.println("Invalid Password.");}
}
else {System.out.println("Invalid User ID.");}
}
public static void closing(){
System.out.println("You signed out.");
pw.close();
}
} // end of program我的.DAT文件
aabbcc-11
Onetwo3
aaabbb 22
Onetwo34
发布于 2014-08-20 15:31:44
感谢所有回复的人。最后,问题是我有了一个“粗略的草稿”java类,然后我将代码复制并粘贴到同一个java项目下的一个新java类中,这似乎给了我一些问题。一旦我创建了一个新的java项目并使用了有效的java类,它就可以正常工作了。我还将pw.flush();方法添加到代码中,以便能够立即而不是在关闭时追加到文件中。还删除了system.exit。
发布于 2014-08-01 00:10:28
更改这一行:
scannerDat.useDelimiter("\r\n");至
scannerDat.useDelimiter("\n");为我工作!
发布于 2014-08-01 00:14:02
你所做的第一件坏事是,你有一个PrintWriter (-)和一个扫描仪(scannerDat)都访问同一个文件,它们都没有关闭对文件的访问,只是在最后,pw关闭了。
isNewId是罪魁祸首。在这里,您最好使用FileReader而不是扫描仪。在方法中在本地声明FileReader,并确保在退出之前文件访问已关闭,此过程。
同样在isNewId中--不要调用System.exit();在这个大小的程序中,它是可以的,但是任何比这个更大的东西都是一个主要的罪过,您不应该像这样不体面地退出一个程序。
https://stackoverflow.com/questions/25070788
复制相似问题