In Java Code: The .txt file is a list of email addresses you collected for a con
ID: 3580975 • Letter: I
Question
In Java Code: The .txt file is a list of email addresses you collected for a conference your company hosted. Create a program that will parse each email address by its name and company. (Note: email parser object is below). The company should be sorted in the first column and the person’s name associated with the company should be sorted in the second column; separated with tab character. (first sort is by company; secondary sort is by firstname.lastname). Create a histogram on the number of attendees per company for the conference. Check if steve.jobs attended the conference and output it. Create an output text file for the output to be imported into a program such as excel.
Email Parser Object:
class Email {
private String email;
Email() {
email = null;
}
Email(String str) {
email = str;
}
public void setEmail(String e) {
email = e;
}
public String getEmail() {
return email;
}
public String getName() {
int pos = email.indexOf('@');
String s = email.substring(0,pos);
return s;
}
public String getCompany() {
int posFirst = email.indexOf('@');
int posLast = email.lastIndexOf('.');
String s = email.substring(posFirst+1,posLast);
return s;
}
}
Sample output :
amzn bob.freed
amzn caitlin.thomas
Explanation / Answer
Hi, Please find my program.
I have implemented all requirement.
Please let me know in case of any issue.
################# Email.java ##################
class Email implements Comparable<Email>{
private String email;
Email() {
email = null;
}
Email(String str) {
email = str;
}
public void setEmail(String e) {
email = e;
}
public String getEmail() {
return email;
}
public String getName() {
int pos = email.indexOf('@');
String s = email.substring(0,pos);
return s;
}
public String getCompany() {
int posFirst = email.indexOf('@');
int posLast = email.lastIndexOf('.');
String s = email.substring(posFirst+1,posLast);
return s;
}
@Override
public int compareTo(Email o) {
// first sorting based on Company Name
int c = getCompany().compareTo(o.getCompany());
if( c == 0) // if company name is same, sort based on name
return getName().compareTo(o.getName());
return c;
}
}
################# EmailParseDetails.java #################
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class EmailParseDetails {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
// list to store email
ArrayList<Email> emailList = new ArrayList<Email>();
// reading file
System.out.print("Enter input file name: ");
String fileName = sc.next();
// opening input file
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String emailStr;
while((emailStr = br.readLine()) != null){
// creating Email Object
Email email = new Email(emailStr);
// adding in list
emailList.add(email);
}
// closing file
br.close();
fr.close();
// sorting emailList
Collections.sort(emailList);
// displaying
System.out.println("Company Name Name");
for(Email e : emailList){
System.out.println(e.getCompany()+" "+e.getName());
}
}
}
/*
Sample output:
Enter input file name: emaillist.txt
Company Name Name
apple alex.bob
sigmoid gaurav.suma
yatra ajay
yatra pravesh.kumar
*/
########### emaillist.txt ##########
pravesh.kumar@yatra.com
alex.bob@apple.usa
gaurav.suma@sigmoid.com
ajay@yatra.com
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.