[Solved] Split Email:passrwords to list of emails and list of passwords [duplicate]


You can use split to first split at all linebreaks, then to split each line at the :. Of course you might run into trouble if the password contains a :, but that is a different task.

    String text = textArea.getText();
    String[] lines = text.split("\r?\n");
    List<String> users = new ArrayList<>(lines.length);
    List<String> passwords = new ArrayList<>(lines.length);
    for(String line:lines) {
        String[] userAndPass = line.split(":");
        users.add(userAndPass[0]);
        passwords.add(userAndPass[1]);
    }

assigning text to an example input of "u1:p1\nu2:p2\r\nu3:p3" and putting a System.out.println(users +"\n"+ passwords); at the end will print:

[u1, u2, u3]
[p1, p2, p3]

4

solved Split Email:passrwords to list of emails and list of passwords [duplicate]