Reputation: 53
I'm supposed to write a program in Java . I'm given a text file containing
I am Hakan. My email address is [email protected], and what is your name? Hi my name is Tarikul and my favorite email address is [email protected]
and im suppossed to take [email protected] and [email protected] and organize them acording to whether or not they have a subdomain(the "cs" part there other possibilities) or not and store it into class arrays of type Email and UniversityEmail. I'm to then take a user iput of 0-7 and depending on the input print out a different set of info I've created the classes and but i dont know how i can take the info then sort it. The possible subdomains are
1.art 2. chee 3. chem 4. coe 5. cs 6. egr 7. polsci
import java.io.* ;
import java.util.HashSet;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Try {
public static void main(String[] args) {
Email [] storage;// email is a class that was made to store the data
storage = new Email [99];
UniversityEmail[] save;
save= new UniversityEmail[99];
HashSet<String> hs = new HashSet<>();
Scanner input= null;
PrintWriter output= null;
try {
input= new Scanner("inputemails.txt");
output= new PrintWriter("outputemails.txt");
}
catch (FileNotFoundException e) {
System.out.print("File not found");
System.exit(0);}
String line = null;
while(input.hasNextLine())
{
fillEmailsHashSet(line, hs);
}
input.close();
output.close();
}
public static void fillEmailsHashSet(String line,HashSet<String> container){
Pattern p = Pattern.compile("([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})");
Matcher m = p.matcher(line);
while(m.find()) {
container.add(m.group(1));
}
}}
These are the actual instructions i received for this program
- Connect to the external input file. The name of the input file will always be inputemails.txt, and therefore please don’t ask the file name in your program.
- Detect the email addresses in the file.
- If an email does not have a sub-domain, please create Email object for that email.
- If an email has a sub-domain, please create UniversityEmail object for that email.
- Please store all emails in the same (one single) array list.
- After you copy all emails from the file to the array list, please ask the user what type of emails to be included in the output file. If the user enters 0, please write the emails that do not have sub-domain in the array list. Please note that the output file must start with a number indicating the number of emails in the file. If the user enters a number between 1-7, please write all emails from the specific department in the output file. Please note that the output file must start with a number indicating the number of emails in the file. The user can enter only one integer number from 0 to 7
I decided to include my other classes info too, this first one is Email class
public class Email {
public String username;
public String email1;
public String extension;
public Email()
{
//default construntor
}
public Email(String U, String Em , String Ex)
{
username =U;
email1= Em;
extension=Ex;
}
}
and here is the code for the UniversityEmail class
public class UniversityEmail extends Email {
public String department;
public int code;
public UniversityEmail()
{//default constructor
}
public UniversityEmail(String U, String Em , String Ex
, String Dep,int C)
{super( U, Em , Ex);
department= Dep;
code=C;
}
}
Upvotes: 1
Views: 144
Reputation: 21586
As long as you only need the Email addresses without any additional data, you can just store them as Strings.
Using a List is much more flexible than using an array. I especially like, that lists can get as long as you want...
I wrote an example, using a Map
as storage. A Map is in general a good way of organizing stuff: You can store data in it by defining "key/value"-pairs.
In my example the map uses subdomains (as Strings) as keys and Lists of eMail-Addresses (again as Strings) as values.
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Try {
public static void main(String[] args) throws IOException {
Map<String, List<String>> storage = new HashMap<>();
// !important! initialize scanner with a file, not with a filename
Scanner input = new Scanner(new File("inputemails.txt"));
// a new pattern to get the subdomain out of an eMail-address
Pattern subdomainPattern = Pattern
.compile(".*\\@([^\\.]+(?:\\.[^\\.]+)*)(?:\\.[^\\.]+){2,}");
// I used your pattern to find any eMail-address
Pattern eMailPattern = Pattern
.compile("([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})");
// Use Scanner to find each match in your input file
String eMailAddress;
while ((eMailAddress = input.findWithinHorizon(eMailPattern, 0)) != null) {
// determine the subdomain of this eMail-address
String subdomain;
Matcher matcher = subdomainPattern.matcher(eMailAddress);
if (matcher.matches()) {
subdomain = matcher.group(1);
} else {
subdomain = "no subdomain";
}
// get/create list of all eMail-addresses for this subdomain from
// map
List<String> listOfEMailAddresesToThisSubdomain = storage
.get(subdomain);
if (listOfEMailAddresesToThisSubdomain == null) {
listOfEMailAddresesToThisSubdomain = new ArrayList<>();
storage.put(subdomain, listOfEMailAddresesToThisSubdomain);
}
// add this eMail-address to list
listOfEMailAddresesToThisSubdomain.add(eMailAddress);
}
input.close();
System.out.println("Choose subdomain");
System.out
.println("0.none 1.art 2. chee 3. chem 4. coe 5. cs 6. egr 7. polsci");
// read, what the user will choose
char userInput = (char) System.in.read();
// map the number to a subdomain name
String chosenDomain;
switch (userInput) {
case '0':
chosenDomain = null;
break;
case '1':
chosenDomain = "art";
break;
case '2':
chosenDomain = "chee";
break;
case '3':
chosenDomain = "chem";
break;
case '4':
chosenDomain = "coe";
break;
case '5':
chosenDomain = "cs";
break;
case '6':
chosenDomain = "egr";
break;
case '7':
chosenDomain = "polsci";
break;
default:
System.out.println("invalid choice");
return;
}
// open file for writing
PrintWriter output = new PrintWriter("outputemails.txt");
// use the chosen subdomain name as a key to our storage map. The result
// will be the list of all eMail-Addresses of that subdomain (or null).
List<String> eMailAddresses = storage.get(chosenDomain);
// make output
if (eMailAddresses == null) {
System.out.println("no addresses");
} else {
for (String eMail : eMailAddresses) {
System.out.println(eMail);
}
}
output.close();
}
}
an input like yours will lead to an output like this:
cs
[email protected]
[email protected]
Upvotes: 1