0

I am learning java.i am trying to find all the unique e-mail addresses present in the paragraph using regex.my code is reading all the input line by line except the last line.i have been trying to figure out what the problem is but i couldn't.i give the integer K which represents number of lines in paragraph.i give k number of lines as input from which my code finds all the unique email address .After finding all the unique email addresses i put them in AraryList and sort and print the output.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static void main(String[] args) {
    int k;
    String result = "";
    Scanner scan = new Scanner(System.in);
    k = scan.nextInt();    //number of lines in a paragraph
    System.out.println("");
    scan.nextLine();
    ArrayList<String> li = new ArrayList<String>();
    Pattern pat = Pattern.compile("\\b[a-zA-Z0-9_.]+\\@[a-z.]+[a-z]+\\b");  //regex to find the email addresses
    for(int iter = 1;iter <=k ; iter++){
        System.out.println("value of iter is "+iter);
        String getline = scan.nextLine();         //reads input lines
        System.out.println(getline);
        Matcher match = pat.matcher(getline);
            while(match.find()){
            String email = match.group(0);
            if(!li.contains(email)){        //if list does not contain the email it adds them
                System.out.println(email);
                li.add(email);
            }
        }
    }
    Collections.sort(li);
    for(String email: li){
        result += email+";";  //removing the semi colon present at the end
    }
    System.out.println(li.size());
    result = result.substring(0,result.length());
    System.out.println(result);
}

}

input
14
Letters to the Editor (Your complete mailing address is required):
letters@thehindu.co.in
Readers' Editor:
readerseditor@thehindu.co.in
Advertisements Queries (Print):
inetads@thehindu.co.in
Advertisements Queries (Online):
digital@thehindu.co.in
Advertisements Queries (International):
international@thehindu.co.in
Subscription Queries:
subs@thehindu.co.in
Comments on the website:
web.thehindu@thehindu.co.in

my ouput
value of iter is 1
Letters to the Editor (Your complete mailing address is required):
value of iter is 2
letters@thehindu.co.in
value of iter is 3
Readers' Editor:
value of iter is 4
readerseditor@thehindu.co.in
value of iter is 5
Advertisements Queries (Print):
value of iter is 6
inetads@thehindu.co.in
value of iter is 7
Advertisements Queries (Online):
value of iter is 8
digital@thehindu.co.in
value of iter is 9
Advertisements Queries (International):
value of iter is 10
international@thehindu.co.in
value of iter is 11
Subscription Queries:
value of iter is 12
subs@thehindu.co.in
value of iter is 13
Comments on the website:
value of iter is 14

it reads till line 13 and waits for the input for the 14th time, but as per my assumption it should read the 14th line from console itself like previous lines did(like for first iter reads first line ,for iteration 2 reads second line and for iter 3 reads third line and so on).why is it not reading the 14 th line.This is confusing.where do i miss the logic ,please enlighten me with brief explanation as i am newbie to java.

0 Answers0