lets say i have a TextArea that contatins Email:password -in that way- in each line. I want to split emails and passwords to list of emails and list of passwords. how can i do that?
Asked
Active
Viewed 118 times
-5
-
1You already mention `split`, have you tried to use it so far? – f1sh Sep 26 '21 at 09:49
-
yes but im not sure how to do it because i usually wirte `mail = textArea1.getText().split("\\r?\\n");` `pass = textArea2.getText().split("\\r?\\n");` to spilt each line and each of them were in a differnt TextArea. my potato head cant comprehend how to do it – Awni Ahmed Sep 26 '21 at 09:59
1 Answers
0
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]

f1sh
- 11,489
- 3
- 25
- 51
-
yeah, if the passwords contain `:` in it i will make it split the text just once. right? – Awni Ahmed Sep 26 '21 at 10:12
-
-
-
@JoakimDanielson because it is not mention in the requirements. Also i consider this a pretty extensive answer already for a "i have tried nothing at all" question. – f1sh Sep 26 '21 at 10:44