Scanner Class(Java)的一个问题

时间:2014-08-04 02:46:40

标签: java java.util.scanner

我整天都在编写这个程序,我开始有点睁眼。我无法弄清问题是什么。

我将datScanner实例化以扫描.DAT文件,当使用扫描程序(system.in)输入新用户名时,newUser模块调用newID模块,该模块应该将新用户名与第一个用户名进行比较它在文件中找到的用户名,看它是否匹配,如果为false则返回true或false,具体取决于答案,它将循环,直到找到匹配或退出。我为此目的设置了一个do-while循环。但是,我在.dat文件中的第一个记录是corey-d93,当我将其输入控制台(scanner.next())时,它允许此条目通过。

简而言之,我要问的是为什么我的程序无法正确区分新用户名和现有用户名。

以下是相关代码:

public static void init() {
try{
    scanner = new Scanner(System.in);
    scanner.useDelimiter("\r\n");
}
catch(Exception e){
    System.out.println("Scanner error!");
}   
try{
    datScanner = new Scanner(new File("loginInfo.txt"));
    datScanner.useDelimiter("\r\n");
}
catch(Exception e){
    System.out.println("File not found!");
}
try {
    pw = new PrintWriter ("loginInfo.dat");
    } 
catch (IOException e) {
    e.printStackTrace();
    System.out.println("Input/Output error");   
    }
}

public static void signIn(){
    String id, pass;
    boolean answer, validPassword;
    System.out.println("Enter your ID");
    id = scanner.next();
    answer = isNewId(id);
    System.out.println("Enter your password");
    pass = scanner.next();
    validPassword = isValPassword(pass);
    if (answer == false){
        if ( validPassword == false){
            System.out.println("Invalid User Password");
        }
        else {
            System.out.println("Authenticated");
        }
    }
}
public static void newUser() {
    boolean validId = true, newId = true, valLen = true, conNum = true, 
            conUpper = true, conLower = true;
    String id, pass;
    do {
        if (validId == false)
            System.out.println("Invalid ID");
        if (newId == false)
            System.out.println("ID taken, enter another");
        System.out.println("Enter an ID that follows the rules");
        System.out.println("Rule 1: 6 letters, followed by a dash, followed by 2 numbers");
        System.out.println("Rule 2: No white spaces or other symbols");
        System.out.println("Rule 3: ID must be unique");
        id = scanner.next();
        validId = isValidId(id);
        newId = isNewId(id);
}
    while(validId == false || newId == false);      
    do {
        if (valLen = false){
            System.out.println("Password length out of range");
        }
        if (conNum = false){
            System.out.println("Password needs a number");
        }
        if (conUpper = false){
            System.out.println("Password is missing an uppercase letter");
        }
        if (conLower = false){
            System.out.println("Password is missing a lowercase letter");
        }
        System.out.println("Enter a password that follows the rules.");
        System.out.println("Rule 1: 6-12 characters in length");
        System.out.println("Rule 2: must contain at least 1 number");
        System.out.println("Rule 3: must contain at least 1 upper case letter");
        System.out.println("Rule 4: must contain at least 1 lower case letter");
        System.out.println("Rule 5: must NOT contain any white spaces or symbols");
        pass = scanner.next();
        valLen = isValLen(pass);
        conNum = isValNum(pass);
        conUpper = isValUpper(pass);
        conLower = isValLower(pass);
        }
    while((valLen == false || conNum == false) || (conUpper == false || conLower == false));
    try{
        pw.println(id);
        pw.println(pass);
    }
    catch (Exception e) {
        e.printStackTrace();
        System.out.println("Input/Output error");
        }
}

public static boolean isValidId (String id){    
    if (id.matches(IDPAT)){
        return true;        
    }
    else {
        return false;
    }
    }


public static boolean isNewId (String id){
    boolean answer = true;
    do{
        String record = datScanner.next();
        String pass = datScanner.next();
        if (record == id){
            answer = false;
            break;
        }
    }
     while (datScanner.hasNext());
    return answer;  
}        

这不是完整的代码,所以如果你们对其余代码有任何疑问,请继续问。我没想到有人会欣赏500行代码,所以我把它简化为绝对必要的。

非常感谢任何和所有帮助!

0 个答案:

没有答案