Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
[2014-08-19 00:01:43,753] REC|SRC:923119511824|DST:9900|CNT:1|OPR:zong|ADD RIDAsMs
[2014-08-19 00:01:32,894] REC|SRC:923142676343|DST:9900|CNT:1|OPR:zong|ADD BBCMEDIA\n\n
[2014-08-19 00:02:42,754] REC|SRC:923119511824|DST:9900|CNT:1|OPR:zong|ADD sMs

here is the text in text file, i want to show the duplicate contact numbers which are after "SRC:" with the contant that is shown after the "ADD" in java. Can any one help please??? i have read the file and stored it into an arraylist, i am beginner to java.
Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class pring
{
    private static ArrayList<string> data = new ArrayList<string>();
    public static void main(String[] args) throws IOException
    {
        arrayListConstructor("C:/Users/DfroJaCk DB/Desktop/zongrecv.txt");
        System.out.println(data);
    }
    public static void arrayListConstructor(String filename) throws IOException
    {
        BufferedReader br = null;
        br = new BufferedReader(new FileReader(filename));
        String line = br.readLine();
        while (line != null)
        {
            data.add(line);
            line = br.readLine();
        }
        br.close();
    }
}
Posted
Updated 25-Aug-14 1:43am
v2
Comments
Shubhashish_Mandal 25-Aug-14 7:46am    
try regex to identify any pattern.

Use on of the String.split() methods, as described in the documentation[^]. You can then compare specific fields of the records.
 
Share this answer
 
Use HashMap:

C#
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class pring
{
    private static ArrayList<String> data = new ArrayList<String>();
    public static void main(String[] args) throws IOException
    {
        arrayListConstructor("C:/Users/DfroJaCk DB/Desktop/zongrecv.txt");
        System.out.println(data);
    }
    public static void arrayListConstructor(String filename) throws IOException
    {
        HashMap<String, String> map = new HashMap<String, String>();

        BufferedReader br = null;
        br = new BufferedReader(new FileReader(filename));
        String line = br.readLine();
        while (line != null)
        {
            /* if line is new key, return null,
             * if line is duplicate key, return value
             */
            if (map.get(line) == null) {
                System.out.println("new: " + line);
                data.add(line);
                map.put(line, line);
            }
            else
                System.out.println("duplicate: " + line);

            line = br.readLine();
        }
        br.close();
    }
}
 
Share this answer
 

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