java The file emails.txt contains a list of email addresses like this: From: jan
ID: 3847355 • Letter: J
Question
java
The file emails.txt contains a list of email addresses like this:
From: janis joplin <kosmicblues@yahoo.com>
From: kurt cobain <lovescourtney@verizon.net>
From: jimi hendrix <meanriffs@altavista.net>
From: john lennon <imagine@hotmail.com>
From: john lennon <imagine@hotmail.com>
From: freddie mercury <bohemianrhapsody@aol.com>
From: kurt cobain <lovescourtney@verizon.net>
We need to convert it to a comma separated list of just the email addresses as such: kosmicblues@yahoo.com,meanriffs@altavista.net, imagine@hotmail.com, bohemianrhapsody@aol.com, lovescourtney@verizon.net
Write a program using streams that will read the emails.txt file and make this conversion, outputting each email address only once. Make sure your output line ends with a linefeed and not another comma-space.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("emails.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (String str : values) {
System.out.println(str);
}
}
br.close();
}
}
Kindly rate my answer.Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.