java - Class arrays and sorting info -


i'm supposed write program in java . i'm given text file containing

i hakan. email address hakan@cs.uh.edu, , name? hi name tarikul , favorite email address tarikul2000@cs.uh.edu

and im suppossed take tarikul2000@uh.edu , hakan@cs.uh.edu , organize them acording whether or not have subdomain(the "cs" part there other possibilities) or not , store class arrays of type email , universityemail. i'm take user iput of 0-7 , depending on input print out different set of info i've created classes , dont know how can take info sort it. possible subdomains

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 class made store 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 actual instructions received program

  1. connect external input file. name of input file inputemails.txt, , therefore please don’t ask file name in program.
  2. detect email addresses in file.
  3. if email not have sub-domain, please create email object email.
  4. if email has sub-domain, please create universityemail object email.
  5. please store emails in same (one single) array list.
  6. after copy emails file array list, please ask user type of emails included in output file. if user enters 0, please write emails not have sub-domain in array list. please note output file must start number indicating number of emails in file. if user enters number between 1-7, please write emails specific department in output file. please note output file must start number indicating number of emails in file. user can enter 1 integer number 0 7

i decided include other classes info too, first 1 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 code 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;      }  } 

as long need email addresses without additional data, can store them strings.

using list more flexible using array. like, lists can long want...

i wrote example, using map storage. map in general way of organizing stuff: can store data in defining "key/value"-pairs.

in example map uses subdomains (as strings) keys , lists of email-addresses (again strings) 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 file, not filename         scanner input = new scanner(new file("inputemails.txt"));          // new pattern subdomain out of email-address         pattern subdomainpattern = pattern                 .compile(".*\\@([^\\.]+(?:\\.[^\\.]+)*)(?:\\.[^\\.]+){2,}");          // used pattern find email-address         pattern emailpattern = pattern                 .compile("([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-za-z]{2,4})");          // use scanner find each match in input file         string emailaddress;         while ((emailaddress = input.findwithinhorizon(emailpattern, 0)) != null) {              // determine subdomain of email-address             string subdomain;             matcher matcher = subdomainpattern.matcher(emailaddress);             if (matcher.matches()) {                 subdomain = matcher.group(1);             } else {                 subdomain = "no subdomain";             }              // get/create list of email-addresses subdomain             // map             list<string> listofemailaddresestothissubdomain = storage                     .get(subdomain);             if (listofemailaddresestothissubdomain == null) {                 listofemailaddresestothissubdomain = new arraylist<>();                 storage.put(subdomain, listofemailaddresestothissubdomain);             }              // add email-address 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, user choose         char userinput = (char) system.in.read();          // map number 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 writing         printwriter output = new printwriter("outputemails.txt");          // use chosen subdomain name key our storage map. result         // list of email-addresses of subdomain (or null).         list<string> emailaddresses = storage.get(chosendomain);          // make output         if (emailaddresses == null) {             system.out.println("no addresses");         } else {             (string email : emailaddresses) {                 system.out.println(email);             }         }         output.close();     } } 

an input yours lead output this:

cs   hakan@cs.uh.edu   tarikul2000@cs.uh.edu 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -