Question

I have a text file that would supposedly look like this

I am Hakan. My email address is hakan@cs.uh.edu, and what is your name? Hi my name is Tarikul and my favorite email address is tarikul2000@uh.edu

im supposed to create a program that will locate the emails in the file store the username,domaian, subdomain and extension then rewrite them int another text file.

import java.io.* ;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileOutputStream;

public class Try {
    public static void main(String[] args) {
        Email [] storage;// email is a class that was made to store the data
        try {
            Scanner input= new Scanner("inputemails.txt");
            PrintWriter output= new PrintWriter("outputemails.txt");

            }

         catch (FileNotFoundException e) {
            System.out.print("File not found");
            System.exit(0);}
        int i=0;
        while(input.hasNextLine()){
            if(input.contains("@"));
            {
                storage[i]=
            }
        }


    }

}

this is what i have so far , its not much but how would i get it to find the email in the text file?

also i thought it be better if i added the actual instructions to my assignment, im not asking for anyone to do the entire progam jus how would i separate the data i need to find

  1. 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.
  2. Detect the email addresses in the file.
  3. If an email does not have a sub-domain, please create Email object for that email.
  4. If an email has a sub-domain, please create UniversityEmail object for that email.
  5. Please store all emails in the same (one single) array list.
  6. 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 8

and the subdomains its is talking about are

1 art 2 chee 3 chem 4 coe 5 cs 6 egr 7 polsci

Was it helpful?

Solution

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));
        }

    }

You can find an example of reading a file here: https://stackoverflow.com/a/22074145/3315914

EDIT:

Example with input/output:

public static void emails() {
        HashSet<String> hs = new HashSet<>();
        FileReader file = null;
        try {
            file = new FileReader(new File("emails.txt"));
        } catch (FileNotFoundException e1) {
            System.err.println("File emails.txt not found!");
            e1.printStackTrace();
        }
        BufferedReader br = new BufferedReader(file);
        String line;
        try {
            while ((line = br.readLine()) != null) {
                fillEmailsHashSet(line, hs);
            }

        } catch (IOException e) {
            System.err.println("Error when reading");
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    System.err.println("Unexpected error");
                    e.printStackTrace();
                }
            }
        }
        for (String string : hs) {
            System.out.println(string);
        }
    }

EDIT (2): A more compact way would be using try-with-resources

public static void emails() throws IOException {
    HashSet<String> hs = new HashSet<>();
    FileReader file = null;
    try (BufferedReader br = new BufferedReader(new FileReader(new File("emails.txt")))) {
        String line;
        while ((line = br.readLine()) != null) {
            fillEmailsHashSet(line, hs);
        }
        for (String string : hs) {
            System.out.println(string);
        }
    }
}

Input file contents:

dolor ac egestas purus TheBumpy@whatever.com Vestibulum justo. magna vulputate Morbi TheBlue@whatever.com
TheJudicious@whatever.com Nulla nec dui. adipiscing in TheOpen@whatever.com TheFascinated@whatever.com
sapien urna mi TheHarmonious@whatever.com vitae aliquam In eget Pellentesque ThePhysical@whatever.com tellus.
non sollicitudin faucibus et mi justo, sit nibh dapibus potenti. venenatis venenatis, molestie sed Proin fermentum elementum.
Sed ut velit venenatis TheDidactic@whatever.com dignissim
consequat condimentum, porttitor Lorem nibh felis,
ullamcorper eros. ut diam vel ipsum nisi luctus. ultrices sem amet non Aliquam aliquet lobortis mauris Vestibulum est purus dignissim
Etiam Cras in eget, Sed pellentesque, ThePhobic@whatever.com eu amet,
Mauris magna euismod, odio semper lorem, potenti. dui tellus.
TheDetailed@whatever.com Ut ipsum mi non Suspendisse tempus cursus ac nec TheMiniature@whatever.com semper. ac, quis suscipit posuere,
posuere Suspendisse Donec tristique a sagittis  vel vitae urna Aliquam vehicula sit condimentum. mi risus mollis rutrum varius. nec elit.
nulla Fusce TheKaput@whatever.com sagittis dictum nunc, eget in TheAmusedGamer@gmail.com venenatis consectetur ultricies. interdum fermentum.
ante TheJolly@whatever.com eros quam. nec TheElectric@whatever.com blandit. massa. molestie ac, TheHistorical@whatever.com purus, ligula fringilla
imperdiet lorem neque. et blandit tortor. enim sed, magna.

Output:

ThePhysical@whatever.com
TheHistorical@whatever.com
TheAmusedGamer@gmail.com
TheBlue@whatever.com
TheKaput@whatever.com
TheMiniature@whatever.com
TheFascinated@whatever.com
ThePhobic@whatever.com
TheBumpy@whatever.com
TheDetailed@whatever.com
TheHarmonious@whatever.com
TheJudicious@whatever.com
TheElectric@whatever.com
TheJolly@whatever.com
TheOpen@whatever.com
TheDidactic@whatever.com

EDIT:

If you want it in the form of array:

String[] array=hs.toArray(new String[hs.size()]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top