0

I have to take this text file:

Ulric Schwartz ullamcorper@Quisque.ca Fringilla Donec PC urna convallis erat
Jesse Conrad Nunc@eunulla.edu Magna Praesent Interdum Incorporated et netus          
et
Ethan Eaton cursus@Nullam.co.uk Sed Consequat Auctor Institute posuere   
vulputate lacus
Griffin Stephenson habitant@mattis.com Purus Sapien Institute auctor non 
feugiat
Alan Howell lorem@penatibusetmagnis.com Mi Limited non sollicitudin a
Sawyer Stokes ornare@utmiDuis.com Ut Institute nibh Phasellus nulla
Nigel Sanford adipiscing@euerat.org Lacus Varius Corp Integer vitae nibh

and scan it for the email addresses, meaning an @ followed by atleast three characters, a period, and atleast two more characters. I understand how to scan the file:

while(fscan.hasNext())
{
    //scan for emails goes in here
}

but I'm not sure how to scan for the email. This is what I have:

import java.io.*;
import java.util.Scanner;

public class lab11_emena {

    public static void main(String[] args)
    {
   Scanner cscan = new Scanner(System.in);
   System.out.println("Please enter the file name.");
   String filename = " ";
   filename= cscan.nextLine();

   File inFile = new File(filename);


            if(!inFile.exists())
            {
            System.out.println("File " + filename + " does not exist.");
            System.exit(0);
            }

            Scanner fscan =  new Scanner(inFile);//I am getting an error     
here? Saying inFile was thrown

System.out.println("Opened file " + filename); 



   }




}
Elizabeth
  • 13
  • 1
  • 7
  • 1
    Look up a regular expression for emails. – tnw Apr 09 '15 at 18:43
  • You can use a regular expression to find e-mail addresses. You can find a detailed regex with Google, e.g. [this](http://www.regular-expressions.info/email.html) – Andy Turner Apr 09 '15 at 18:43
  • You want to do something similar to what has been answered at:http://stackoverflow.com/questions/8204680/java-regex-email – Math Apr 09 '15 at 18:44
  • 1
    http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/ – Amir Afghani Apr 09 '15 at 18:44
  • If is not ALL emails but only those that have `@ followed by atleast three characters, a period, and atleast two more characters..` then you can use something like `String pseudoRegex = "\\w+@\\w{3,}\\.\\w{2,}"` then Just check `fscan.next().matches(pseudoRegex)` – gtgaxiola Apr 09 '15 at 18:46
  • @gtgaxiola would i put that in the while(fscan.next()etx) – Elizabeth Apr 09 '15 at 19:13
  • @Elizabeth I would assume yes. But take in account you have to split your `line` to word tokens prior to checking if it matches. – gtgaxiola Apr 09 '15 at 19:14

3 Answers3

0

You must use a scanner to read the characters. Then check for the different requirements for each thing like the @ character. So if char=="@" then it will continue looking for the other requirements. Then make it go forwards and backwards, untill it finds the spaces at either end of the email, then you can import all of the characters between them.

D3sast3r
  • 1
  • 3
  • what statement do I use to scan the character? I know to scan in between each space and to print out for example, if the text file was name 23 i would use: while(fscan.hasNext()) { //code in here} – Elizabeth Apr 09 '15 at 19:00
0

I would first recommend using a delimeter between the different pieces of information (i.e. a comma).

Example Ulric Schwartz, ullamcorper@Quisque.ca, Fringilla Donec, PC urna convallis erat

Now if all your lines will have the same number of "categories (info between each comma)" of information (the above example would have 4). Then you can load each item into an array and then pull out #2,6,10, etc..

If the categories will vary then you would have to do as D3sast3r stated, find the @, then scan forwards and backwards to the spaces.

  • unfortunately, I have to use this text file as it is, because that is what we are provided with – Elizabeth Apr 09 '15 at 18:53
  • @Elizabeth Other people on here are suggesting regex, but have you tried loading the entire file into an arraylist, the elements separated by the spaces, then run a for loop over the array with an if statement, if(arraylist.get(i).contains(specialCharacter). – Michael Morgan Apr 09 '15 at 19:04
  • Im not sure how load each element into an array. I've only ever used an array with user input. – Elizabeth Apr 09 '15 at 19:26
0

Try something like this.

Scan the entire file into an arraylist. arrays by default use whitespace as a delimiter, since there is no whitespace in a valid email address you will be fine.

while(inputFile.hasNext()) {
    ArrayList.add(inputFile.next());
}

This put every character into an element of the array, using spaces to separate them. So element 0 = Urlic, element 1 = Schwartz, etc... Now you can use a regex object as gtgaxiola suggested to compare each element of the array too

String email = "\\w+@\\w{3,}\\.\\w{2,}";

This is basically an string object based on your requirements. "stuff" then a @ symbol then at least 3 characters then a period then at least 2 more characters

Now search the array with a for loop and an if statement

for(i = 0; i < ArrayList.length(); i++) {
    if(ArrayList.get(i).contains(email) {
        //do something with the email address
    }
}