java - While 循环在检查输入之前结束

标签 java if-statement while-loop

现在已经四天了,我是初学者,我似乎无法完成这项工作。 因此,到目前为止,我的程序会询问用户名并在我打开的文件中查找匹配项。如果找到匹配,它将询问用户密码,如果在文件中找到用户密码,它将检查用户名、密码,如果行上有特定单词以及凭据,它将打开一个凭据文件。 因此,如果第一次正确输入用户名,我的代码就会工作,它会要求输入密码,然后中断或让用户查看文件。效果非常好。

但是,如果第一次尝试时用户名不正确,则它不会检查第二次是否正确。它只是在 2 次尝试后结束(应该是 3 次失败的尝试)。 这是我的代码

public static void main(String[] args)throws Exception {    
        Scanner scnr = new Scanner(System.in);
        //open credentials file
        FileInputStream in = new FileInputStream ("./credentials.txt");
        Scanner credentials = new Scanner(in);

         // open zookeeper file
        FileInputStream in1 = new FileInputStream ("./zookeeper.txt");
        Scanner zookeeperInfo = new Scanner(in1);

        FileInputStream in2 = new FileInputStream ("./admin.txt");
        Scanner adminInfo = new Scanner(in2);

        FileInputStream in3 = new FileInputStream ("./veterinarian.txt");
        Scanner vetInfo = new Scanner(in3);

        String userName     = "";
        String userPassword = "";
        String original     = ""; 
        int numAttempts = 0;
        boolean run = true;
        while (run) {        
          System.out.println ("User Name or Q: ");
          userName = scnr.nextLine();

          if (userName.equals("Q") || numAttempts > 3) {
            run = false;
            System.out.println("Goodbye..");
          }

          else {
            while(credentials.hasNextLine()) {
              String line = credentials.nextLine();

              if (line.contains(userName)) {
                System.out.println(userName);
                System.out.println("Gimme the password: ");
                userPassword = scnr.nextLine();
                original = userPassword;

                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(original.getBytes());
                byte[] digest = md.digest();
                StringBuffer sb = new StringBuffer();
                for (byte b : digest) {
                  sb.append(String.format("%02x", b & 0xff));
                }

                if (line.contains(userName) && line.contains(sb.toString()) && line.contains("zookeeper")) {
                  while (zookeeperInfo.hasNextLine()) {
                    System.out.println(zookeeperInfo.nextLine() + " ");
                  }
                  break;
                }
                else if (line.contains(userName) && line.contains(sb.toString()) && line.contains("admin")) {
                  while (adminInfo.hasNextLine()) {
                    System.out.println(adminInfo.nextLine()+ " ");
                  }
                  break;
                }
                else if (line.contains(userName) && line.contains(sb.toString()) && line.contains("veterinarian")) {
                  while (vetInfo.hasNextLine()) {
                    System.out.println(vetInfo.nextLine() + " ");
                  }
                  break;
                }                                
              }                                                         
            }

Picture of working part of the code Picture of non-working part

我真的不知道。我觉得我什至没有做得正确,但我所有的尝试都在这里结束。我必须在周日之前提交,但整整一周之后都没有任何效果。 请帮忙,任何建议将不胜感激!

最佳答案

除了你的代码不容易阅读之外,我还为你做了一些你可能会使用的东西:

public static void main(String[] args) throws Exception {
    Scanner userInput = new Scanner(System.in);

    File file = new File("credentials.txt");
    FileInputStream inputStream;

    Scanner credentialsScanner;

    File infosFile;
    Scanner infoScanner = null;
    FileInputStream infosInputStream;

    boolean run = true;
    int attempts = 0;

    String username;
    String password;
    String line;

    while (run) {
        System.out.println("Give username:");
        username = userInput.nextLine();

        if (username.equals("Q") || attempts > 3) {
            run = false;
        } else {
            inputStream = new FileInputStream(file);
            credentialsScanner = new Scanner(inputStream);
            while (credentialsScanner.hasNextLine()) {
                line = credentialsScanner.nextLine();
                System.out.println(line);
                if (line.contains(username)) {
                    System.out.println("Give password:");
                    password = userInput.nextLine();

                    MessageDigest md = MessageDigest.getInstance("MD5");
                    md.update(password.getBytes());
                    byte[] digest = md.digest();
                    StringBuffer sb = new StringBuffer();
                    for (byte b : digest) {
                        sb.append(String.format("%02x", b & 0xff));
                    }

                    if (line.contains(sb.toString())) {
                        if (line.contains("zookeeper")) {
                            infosFile = new File("zookeeperInfo.txt");
                        } else if (line.contains("admin")) {
                            infosFile = new File("adminInfo.txt");
                        } else if (line.contains("veterinarian")) {
                            infosFile = new File("vetInfo.txt");
                        }

                        infosInputStream = new FileInputStream(infosFile);
                        infoScanner = new Scanner(infosInputStream);
                        while (infoScanner != null && infoScanner.hasNextLine()) {
                            System.out.println(infoScanner.nextLine() + " ");
                        }
                        attempts = 0;
                        break;
                    }
                }
            }
            attempts++;
        }
    }
}

如您所见,我简化了您的代码:

  • 一个主循环:
    • 在其中,我们获取用户名(用户输入)
      • 如果用户输入是“Q”或者是第四次尝试 => 打破循环
      • 否则=>继续

当我们继续这个过程时:

  • 对于凭证文件的每一行:
    • 如果包含给定的用户名 => 询问密码

接下来是使用MD5解码器进行密码验证。然后,如果当前lin包含解码后的密码,则处理其他文件信息的读取。

每次尝试后,我们都会增加尝试计数器,但如果该过程成功,我们会重置该计数器并中断循环。

它可能不完全是您的目的,但它可能会帮助您改进代码并更多地理解一些逻辑。

关于java - While 循环在检查输入之前结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51901548/

相关文章:

java - 将纬度和经度值转换为 double 值

java - 如何在 android 中运行多个 HTTP 连接?

javascript - 无限 While 循环 mod 3

linux - 在 "while read"表达式中使用 sed

javascript - IE 条件检查

php - 虽然循环使用单选按钮为许多用户将值插入数据库

java - 如何在接口(interface)中初始化对象?

Java Swing 获取输入

javascript - 优化 if/else 语句

javascript - 在 if 语句中排除所有其他事件 ID