【问题标题】:Why am I getting a false value at if (users.contains(user))?为什么我在 if (users.contains(user)) 处得到错误值?
【发布时间】:2015-05-12 04:14:34
【问题描述】:

我认为 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 文件中的内容。 :

【问题讨论】:

  • 请正确格式化代码。
  • users 在您发布的调试器窗口中的大小为 0
  • if (goahead) { 始终是 true,所以 if (users.contains(user)) { 实际上从未被评估...
  • 谢谢,这些答案确实有道理。我不知道如何解决它。 (我是新人)
  • 你在追逐你的尾巴,你试图加载文件并填充users Set 同时试图确定用户提供的值是否在列表中...... ??填充Set 然后检查输入是否存在或通过读取文件检查用户是否存在...

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


【解决方案1】:

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

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。没有?

【讨论】:

    【解决方案2】:

    您似乎在追逐自己的尾巴,同时做两件事,读取文件/填充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...?
        }
    

    【讨论】:

    • 同意。一次做这么多事情也会让其他人感到困惑。很难理解其意图。
    • @akhil_mittal 我仍在试图弄清楚代码“假设”要做什么,但这是我最好的猜测:P
    • 我会玩弄这个,看看我能想出什么。谢谢
    • @CAPTiNDANCE 请确保您也花一些时间来格式化代码。它只会帮助您更快地获得好的解决方案:)
    • @akhil_mittal 我会更好地格式化它。我仍处于弄清楚如何编写代码的阶段,尽管我想擅长这一点。谢谢大家的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2016-11-25
    • 2017-12-06
    相关资源
    最近更新 更多