Click here to Skip to main content
14,825,178 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program that scans a text file for possible e-mail addresses and writes all email address to an output file.

Addresses look like this:

What I have tried:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class EmailidExtration {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(;
        String input = scanner.nextLine();
        Pattern pattern = Pattern.compile("([a-z0-9_.-]+)@([a-z0-9_.-]+[a-z])");
        Matcher matcher = pattern.matcher("emailtext.txt");
Updated 16-Nov-20 20:52pm
Patrice T 16-Nov-20 20:58pm
What the problem is with this code ?
Member 14994674 17-Nov-20 9:10am
it outputs runs but do not return a thing
Patrice T 17-Nov-20 9:53am
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

Your code finds nothing, because your matcher is working on the filename, not the file content:
Matcher matcher = pattern.matcher("emailtext.txt");
Read the file, and pass the content to the matcher instead of the filename.

And a better email Regex is:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900