Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java.txt用户名和密码不起作用_Java_Netbeans_Passwords - Fatal编程技术网

Java.txt用户名和密码不起作用

Java.txt用户名和密码不起作用,java,netbeans,passwords,Java,Netbeans,Passwords,我只是在试验java(NetBeans),我想出了一个快速的基于文本的冒险游戏。我试图让它在两个文本文件“users.txt”和“passwords.txt”中检查您的用户名和密码,我遵循了一个编程指南 这是进口货 import java.io.*; import javax.swing.JFrame; 这就是错误所在 private void loginActionPerformed(java.awt.event.ActionEvent evt) {

我只是在试验java(NetBeans),我想出了一个快速的基于文本的冒险游戏。我试图让它在两个文本文件“users.txt”和“passwords.txt”中检查您的用户名和密码,我遵循了一个编程指南

这是进口货

import java.io.*;
import javax.swing.JFrame;
这就是错误所在

        private void loginActionPerformed(java.awt.event.ActionEvent evt) {                                      
       String usernametxt = "users.txt";
       String passwordtxt = "passwords.txt";  
       String user = null;
       String pass = null;
       try {
       // file reader for username \\
           FileReader fileReader = new FileReader(usernametxt);
           // file reader for password \\
           FileReader fr = new FileReader(passwordtxt);
           // buffered reader for username \\
           BufferedReader bufferedReader = new BufferedReader(fileReader);
           // buffered reader for password \\
               BufferedReader br = new BufferedReader(fr);
                // check for if user doesn't equal null \\
           while((user = bufferedReader.readLine()) != null){
               // if username equals first line of username.txt \\
               if (username.getText().equalsIgnoreCase(user)){
                   // check for if pass doesn't equal null \\
                   while((pass = br.readLine()) != null){
                   // if password equals first line of passwords.txt \\
                   if (password.getPassword().equals(pass)){
                       // if password = pass than it will exit \\
                   System.exit(1);
                }
                   // else continue \\
                   else{
                       continue;
                   }

               }
           }
       }
       bufferedReader.close();
       br.close();
   }
   catch(FileNotFoundException ex){
      System.out.println("Unable to open file ");
   }
   catch(IOException ex){
       System.out.println("Error reading file");
       }
    }    
这是文本文件

users.txt
matthew

passwords.txt
matt
这里有完整的代码

这里有最新的代码

请随意在这里问我问题

提前感谢您的帮助

最新代码

private void loginActionPerformed(java.awt.event.ActionEvent evt) {                                      
   String usernames = username.getText();
   String passwords = password.toString();
   boolean signedin = false;
   String usernametxt = "users.txt";
   String passwordtxt = "passwords.txt";  
   String user = null;
   String pass = null;

   try {
       FileReader fr1 = new FileReader(usernametxt);
       FileReader fr2 = new FileReader(passwordtxt);

       BufferedReader br1 = new BufferedReader(fr1);
       BufferedReader br2 = new BufferedReader(fr2);

       System.out.println("Username: "+br1.readLine());
       System.out.println("Password: "+br2.readLine());
//While循环未运行(不是if语句错误\

       while ((user = br1.readLine()) != null){
           // checks if username is not equal to usernames.txt \\
           if (user.equalsIgnoreCase(usernames)){
               System.out.println("while loop running, username (right)");
               break;
               }
               else{
               System.out.println("while loop running, username (wrong)");
               }
           }
       br1.close();


           while ((pass = br2.readLine())!= null){
               if (pass.equalsIgnoreCase(passwords)){
                 signedin = true;
                 System.out.println("While loop running, password (right)");
                 break;
              }
              else{
                System.out.println("While loop running, password (wrong)
              }
       }
        br2.close();
//注释掉if语句,因为我不想在测试时关闭\

       //    if (signedin){
           //        System.out.println("SIGNEDIN = TRUE");
          //        new error1().setVisible(true);
        //       this.dispose();
   //   }
    //   if (!signedin){
     //      System.out.println("SIGNEDIN = FALSE");
     //      System.exit(1);
      // }

   }
   catch(FileNotFoundException ex){
       System.out.println("Unable to open file ");
   }
   catch(IOException ex){
       System.out.println("Error reading file");
   }
}                          
新问题


while循环不运行,当它不打印“while loop running,User/Pass”时会确认这不是if/then语句错误,因为我添加了else语句来打印用户名是否正确。请帮助,谢谢Matthew。

如果我理解正确:

两个文件:

  • users.txt保存用户名
  • passwords.txt保存密码
我们希望:

  • 继续读取用户文件,直到文件结束或找到用户
  • 读取密码文件上的相应行,并检查密码是否匹配
  • 检查代码,
    while((user=bufferedReader.readLine())!=null)
    很好地完成了第一部分。我们确实希望继续阅读整个文件,试图找到我们的用户,对吗

    但是嵌套的while看起来有点可疑。我们只需要检查给定用户的单个密码,对吗?对吗

    深入研究您的代码,我们可以看到:

    if (password.getPassword().equals(pass)) {
        // if password = pass than it will exit
        System.exit(1);
    } // plus Lots of code...
    
    嘿!我不认为
    系统。退出
    符合您的要求

    System.exit
    将退出程序,返回DOS或其他酷酷的孩子们现在正在使用的程序。它返回的整数称为错误代码,可用于将信息反馈给启动我们程序的终端/外壳

    您最可能要查找的关键字是
    break
    :它将立即退出给定的循环,不会询问任何问题

    让我们做一些中断/继续mashup!如果密码确实正确,假设我们“在生活中获胜”:

    boolean winAtLife = false;
    while((user = bufferedReader.readLine()) != null){
        String candidatePassword = br.readLine();
        if (candidatePassword == null) {
            // So the password file is shorter than the userfile? 
            // We probably want to log or alert the poor DevOp guys. 
            // throwing an exception seems like the right thing to do here!
            break;
        }
    
        if (!user.equalsIgnoreCase(username.getText())) {
            // These are not the droid we're looking for, Better luck next line!
            continue;
    
            // Also notice that, since we KNOW that user can't be null,
            // we're using the force to save ourselves from dreaded NullPointerExceptions!
        }
    
        if (!candidatePassword.equals(password.getPassword())) {
            // Hmmm, wrong password, I guess?
            // Not sure what do do next, but we DO NOT need to keep looping
            // since we've found our droid/user/whatever.
            // So let's break and save some EC2 Cycles.
            break; 
        }
    
        // If we ever reach here, we got ourselves a winner!
        pass = candidatePassword
        winAtLife = true;
    }
    
    编辑:好的……我听说:

  • 现在的酷孩子用扫描仪
  • 自动关闭资源有利于我们的健康
  • 一些关于分离关注点和将域代码与UI混合的东西。在代码示例中。随便什么
  • 现在我们开始,取两个,作为一种方法:

    public boolean checkCredentials(String username, String password) throws IOException {
        // these two are begging to be constants or inlined. 
        final String usernametxt = "users.txt";
        final String passwordtxt = "passwords.txt";
    
        if (username == null || password == null) {
            // You probably don't want this in production code.
            // Exceptions are your best friends when something unexpected occurs.
            return false;
        }
    
        try (final Reader fileReader = new FileReader(usernametxt);
             final Reader passwordReader = new FileReader(passwordtxt)) {
    
            Scanner userScanner = new Scanner(fileReader);
            Scanner passwordScanner = new Scanner(passwordReader);
    
            while(userScanner.hasNext()) {
                final String user = userScanner.next();
                if (!passwordScanner.hasNext()) {
                    // So the password file is shorter than the userfile?
                    // We probably want to log or alert the poor DevOp guys.
                    // throwing an exception seems like the right thing to do here!
                    return false;
                }
                final String candidatePassword = passwordScanner.next();
                if (!user.equalsIgnoreCase(username)) {
                    // This is not the droid we're looking for
                    // Also notice that, since we KNOW that user can't be null,
                    // we're using the force to save ourselves
                    // from dreaded NullPointerExceptions!
                    continue;
                }
    
                if (!candidatePassword.equals(password)) {
                    // Hmmm, wrong password, I guess?
                    // Not sure what do do next, but we DO NOT need to keep looping
                    // So let's return early and save some EC2 Cycles.
                    return false;
                }
    
                // If we ever reach here, we got ourselves a winner!
                return true;
            }
        } // yay for autocloseable
        return false;
    }
    

    你能提供堆栈跟踪吗?这不是一个很好的代码实现。你正在使用嵌套的while循环。你可以先从单独列表中的两个文件中检索所有用户名和密码,然后开始进行比较。好吧,我试试看,我如何获得堆栈跟踪?我不知道当你的代码出现问题时,会出现什么情况,t如果你必须告诉我们,错误/异常是什么以及它们发生在哪里。同时发布Stacktrace。如果你不知道从哪里获取,那么用谷歌搜索类似“[IDE名称]Stacktrace”的内容。我现在就试试,你看到更新的代码了吗?没有,我正忙着做《星球大战》的笑话。:)1。
    username.getText()
    看起来像什么?2.我假设这两个文件都存在并且可读,好吗?:)由于您正在执行两个不同的循环,因此如果密码在密码文件中,则它将匹配,而不管您喜欢哪个循环。这就是你想要的吗?两个文件都存在,users.txt有一个用户“matthew”,passwords.txt有一个密码“matt”。用户名是输入文本的jtextfield。现在我只做一个循环。现在我只是在试验txt文件。我已经编辑了答案,以提高一些酷的因素。现在是21世纪,办公室里的人一直在谈论扫描仪之类的东西。无论如何,祝你的代码好运:)