【问题标题】:Loop through the file as long as the userinput is same as string in the file只要用户输入与文件中的字符串相同,就遍历文件
【发布时间】:2016-10-23 13:28:57
【问题描述】:

嘿,我正在处理银行帐户申请,目前正在登录。我希望程序通过该文件并检查是否已经有一个与用户想要的用户名相同的用户名,如果有,它应该返回并再次要求一个新的用户名。我尝试了许多不同的方法,但我找不到一种有效的方法。我希望有一个人可以帮助我。 :-)

    String file = "C:\\Users\\dsociety\\IdeaProjects\\bankaccount\\logins.txt";
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    BufferedReader on = new BufferedReader(new InputStreamReader(System.in));
    String username;
    String pw;


    Boolean exists = false;
    Scanner scanner = null;
    try {
        scanner = new Scanner(new File(file));
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }


    System.out.println("Please enter a username");
    username = in.readLine();

    BufferedReader bru = new BufferedReader(new FileReader(file));

    String line;

    try {
        while (exists == true) {
            while ((line = bru.readLine()) != null) {
                while (scanner.hasNextLine()) {
                    String[] userAndPw = scanner.nextLine().split(":");
                    String user = userAndPw[0];

                    if (user.equals(username)) {
                        System.out.println("There is already a User with that username, please try a other username");
                        exists = false;
                    }
                    else {
                        exists = true;
                    }
                }
            }
        }

        bru.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    System.out.println("Please enter a password");
    pw = on.readLine();

【问题讨论】:

    标签: java while-loop


    【解决方案1】:

    我查看了您的代码,正如其他人所注意到的,您无法进入第一个 while 循环,因为 exists 变量为假。我认为你有点搞乱了whiles 的顺序。 您绝对不需要Scanner BufferedReader(其中一个就足够了)来读取文件,就像您不需要两个不同的BufferedReaders 来读取一样从系统输入(同样,一个就足够了)。 我稍微修改了你的代码并添加了一些 cmets,我希望它现在对你有用:

        String file = "C:\\logins.txt";
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String username;
        String pw;
    
        System.out.println("Please enter a username");
    
        BufferedReader bru = new BufferedReader(new FileReader(file));
        // we will keep the users here instead of reading the file everytime a username is entered
        List<String> users = new ArrayList<>();
    
        Boolean exists = null;
        String line;
    
        // populate the existing users list by reading all the lines in the file
        while ((line = bru.readLine()) != null) {
            users.add(line.split(":")[0]);
        }
    
        bru.close();
    
        // we can enter the while loop if we are asking for the username for the first time
        // or if the username entered already exists
        while (exists == null || exists == true) {
            // we read the username from the input
            username = in.readLine();
            // we suppose it doesn't already exist
            exists = false;
            // we look at every existing user
            for (String user : users) {
                /*
                 * if the names are the same, we don't need to look further, we know the username
                 * exists and so we will break from the "for" loop and ask for a username once again
                 * (side note: I used replaceAll in order to remove all invisible unicode characters
                 * that might make two strings unequal that would otherwise be identical)
                 */
                if (user.replaceAll("\\P{Print}", "").equals(username.replaceAll("\\P{Print}", ""))) {
                    System.out.println("There is already a User with that username, please try a other username");
                    exists = true;
                    break;
                }
            }
    
        }
    
        System.out.println("Please enter a password");
        pw = in.readLine();
    

    【讨论】:

      【解决方案2】:

      最好把工作分成两部分:

      1. 扫描文件以查找每个用户名。在地图中输入用户名key
      2. 要求输入用户名,并检查用户名是否存在。

        if (myMap.containsKey(username) == true) {
           // ...
        } else {
           // Go back to step 2
        }
        

      【讨论】:

        【解决方案3】:

        我希望这段代码能帮助您解决问题:

        private static boolean userExists(String consoleParam){
        
            String filePath = "C:\\Users\\dsociety\\IdeaProjects\\bankaccount\\logins.txt";
            File file = null;
            FileReader fr = null;
            BufferedReader br = null;
            Boolean exists = false;
        
            try{
        
                file = new File (filePath);
                fr = new FileReader (file);
                br = new BufferedReader(fr);
        
                String line;
                while((line=br.readLine())!=null){
                    if(line.split(":")[0].equals(consoleParam)){
                        exists = true;
                        break;
                    }
                }
        
            }catch(Exception e){
                 e.printStackTrace();
            }finally{
               /* Close opened resources */
               try{                    
                  if( null != fr ){   
                     fr.close();     
                  }
        
               }catch (Exception e2){ 
                  e2.printStackTrace();
               }  
            }
        
            return exists;
        
        }
        
        public static void main(String[] args) {
        
            Scanner reader = new Scanner(System.in);
            boolean newUser = false;
        
            /* Check if username alredy exists */
            while(!newUser){
        
                System.out.println("Please enter a username"); 
                String username = reader.next();
                newUser = !userExists(username);
        
            }
        
            /* Close resources */
            System.out.println("New username entered!");
            reader.close();
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-27
          • 2016-07-21
          • 2020-12-28
          • 2021-01-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多