0

so I'm working on a project where I'm trying to read thousands of lines from a text file in this format:

EMAILADDRESS1@EXAMPLE.COM     1209381231
EMAILADDRESS2@EXAMPLE.NET     1241231231
EMAILADDRESS3@EXAMPLE.ORG     1231585849
EMAILADDRESS4@EXAMPLE.COM     2132389558
...

etc. etc. ....

Now, I want to read each email address into a string and completely ignore the numbers that comes after it, they aren't important to me. The numbers are separated from the email addressed by spaces.

So, what would be the best way to read each email address into their own string?

dertkw
  • 7,798
  • 5
  • 37
  • 45

4 Answers4

0

Look at the split() method. You specify a character and it splits the string based on that character. The parts of the string which were delimited by the character then get put into an array, which the method returns to you.

Then, assuming that there are no space characters before the email address starts, you can take the 0th element of the array and do with it what you like. Put it into a List, stream the list into a file, whatever.

ALester
  • 63
  • 4
0

Use the split() method! It is definitely the best way of doing it! Other ways can be harder or take up more performance!

String[] strings = myString.split(" ");
Victor2748
  • 4,149
  • 13
  • 52
  • 89
0

You can use:

String str = "HERE YOUR CONTENT";
String[] mailArray = str.split("\\s+\\d+");

If you print mailArray it will contain:

mailArray => 
(
    EMAILADDRESS1@EXAMPLE.COM,
    EMAILADDRESS2@EXAMPLE.NET,
    EMAILADDRESS3@EXAMPLE.ORG,
    EMAILADDRESS4@EXAMPLE.COM
)
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
0

You can try using java scanner class

String str = "name of your text file"
Scanner scan = new Scanner(new File(str));

ArrayList<String> emails = new ArrayList<String>();

Pattern pattern = //some email regex
String tmp = ""
while(tmp = scan.next(pattern)){

emails.add(tmp);

}
TPEACHES
  • 319
  • 3
  • 7