只要userinput与文件中的字符串相同,就循环遍历文件

时间:2016-10-23 13:28:58

标签: java while-loop

嘿我正在开设银行帐户申请,目前我正在登录。我希望Programm浏览该文件并检查是否已存在与用户想要的用户名相同的用户名,如果有用户名,则应返回并再次请求新的用户名。我尝试了很多不同的方法,但我找不到有效的方法。我希望有一个人可以帮助我。 :-)

    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();

3 个答案:

答案 0 :(得分:0)

我查看了您的代码,并且正如其他人注意到的那样,由于exists变量为false,您无法进入第一个while循环。而且我认为你稍微混淆了while s的顺序。 你肯定不需要Scanner 一个BufferedReader(其中一个就足够了)来阅读文件,就像你不需要两个不同BufferedReader 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(); 1}}从系统输入读取(再次,一个就足够了)。 我稍微修改了你的代码并添加了一些注释,我希望它现在对你有用:

java.rmi.server.codebase

答案 1 :(得分:0)

将工作分为两部分会更好:

  1. 浏览文件中的每个用户名。将用户名<kbd>放入地图中。
  2. 询问输入用户名,并检查用户名是否存在。

    key

答案 2 :(得分:0)

我希望此代码有助于解决您的问题:

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();

}