为什么我在if(users.contains(user))上得到一个假值?

时间:2015-05-12 04:14:35

标签: java authentication java.util.scanner logical-operators login-control

我认为user.contains不会读取每一行而只是检查第一行。我之前有这个工作(我正在测试我的代码的重复用户部分),但现在我的程序正在跳过:

{    
            JOptionPane.showMessageDialog(null, "Duplicate user found."); 
            goahead=false;
            dispose();
            }

我不确定我做了什么,或者我是如何设法打破自己的程序的。现在它一直跳到:

else { if (hs.contains(new String(un+" "+pw))) { 
                JOptionPane.showMessageDialog(null,"User, Found Access Granted!");
                    dispose();
                }

我哪里出错了?

private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {                                       
    String un = UserName.getText().trim();
        String pw = Password.getText().trim();
        HashSet hs= new HashSet();
        HashSet users = new HashSet(); 
        boolean goahead=true; 
        try {
            Scanner Scan = new Scanner(new File("Login.txt"));
            while (Scan.hasNextLine()) { 
                String authenticator = Scan.nextLine().trim();
                String[] autparts=authenticator.split(" "); 
                String user = autparts[0].trim();
                if (goahead){
                    if (users.contains(user)) {
                        if (user.equals(un)) {    
                            JOptionPane.showMessageDialog(null, "Duplicate user found."); 
                            goahead=false;
                            dispose();
                        }
                    } else {
                        hs.add(authenticator);
                        users.add(user);
                    }
                }
            }
        } catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
        if (goahead) {
            if (createAccount.isSelected() & (hs.contains(new String(un+" "+pw)))){
                JOptionPane.showMessageDialog(null,"User Already Exsist! No Need to create a new account. ");
                dispose();
            } else { 
                if (hs.contains(new String(un+" "+pw))) { 
                    JOptionPane.showMessageDialog(null,"User, Found Access Granted!");
                    dispose();
                }  else { 
                    if (createAccount.isSelected()){
                        try {
                            PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("Login.txt", true)));

                            output.println(un+" "+pw);
                            output.close(); 
                        } catch (IOException ex) {
                            System.out.printf("error %s/n", ex );
                        }   
                        JOptionPane.showMessageDialog(null,"Welcome!"+" " + un+" "+"Please Relogin Now");
                        dispose();
                    }else {
                        JOptionPane.showMessageDialog(null, "user doesn't exist or password incorrect. "); 
                        dispose();
                    }
                }
            }
        }

以下是我的输出和txt文件中的内容。 :screen shot of the debug results and txt file

2 个答案:

答案 0 :(得分:0)

我现在已经格式化了你的代码:

String un = UserName.getText().trim();
if (goahead){
       if (users.contains(user)) 
       {
         if (user.equals(un)) 
         {    
             JOptionPane.showMessageDialog(null, "Duplicate user found."); 
             goahead=false;
             dispose();
         }
       } 
       else 
       {
        hs.add(authenticator);
        users.add(user);
       }
}

现在,当标记goahead设置为false时,我们不需要将其重置为true某处吗?如果HashSet包含用户,那么为什么还需要再次比较用户名?您应该为用户本身定义equals。都能跟得上?

答案 1 :(得分:0)

你似乎在追逐你的尾巴,在一个人做两件事,阅读文件/填充Set并检查重复。相反,一次做一个......

像...一样的东西。

    // You could pre-load these and cache the result instead
    // of reading it each time, but that's up to you
    Map<String, String> credentials = new HashMap<>(25);
    try (Scanner scan = new Scanner(new File("Login.txt"))) {

        while (scan.hasNextLine()) {
            String value = scan.nextLine();
            String[] parts = value.split(" ");
            credentials.put(parts[0], parts[1]);
        }

    } catch (IOException exp) {
        exp.printStackTrace();
    }

    // Make your required checks here
    if (credentials.containsKey(un)) {
        if (credentials.get(un).equals(pw)) {
            // Validated
        } else {
            // Wrong password
        }
    } else {
        // New user...?
    }