Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners display repeated lines in a file
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


dolly
display repeated lines in a file
1 Answer(s)      3 years and 6 months ago
Posted in : Java Beginners


i want a java program that accepts file from user and displays the repeated lines in a file


Advertisement
View Answers

May 30, 2011 at 5:33 PM


import java.io.*;
import java.util.*;
class  ReadDuplicateLinesFromFile
{
    public static <T> List getDuplicate(Collection<T> list) {

    final List<T> l = new ArrayList<T>();
    Set<T> set = new HashSet<T>() {
    public boolean add(T e) {
        if (contains(e)) {
            l.add(e);
        }
        return super.add(e);
    }
   };
   for (T t : list){
        set.add(t);
    }
    return l;
}

public static <T> boolean hasDuplicate(Collection<T> list) {
    if(getDuplicate(list).isEmpty())
        return false;
    return true;
}
    public static void main(String[] args) throws Exception
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter file name: ");
        String filename=input.next();
        File f=new File(filename);
        BufferedReader br=new BufferedReader(new FileReader(f));
        ArrayList list=new ArrayList();
        String st="";
        while((st=br.readLine())!=null){
            list.add(st);
        }
        System.out.println("Duplicate Elements are: ");
        if(hasDuplicate(list)){
            List l=getDuplicate(list);
            for(int i=0;i<l.size();i++){
                System.out.println(l.get(i).toString());
            }
        }
    }
}


Related Tutorials/Questions & Answers:
display repeated lines in a file
display repeated lines in a file  i want a java program that accepts file from user and displays the repeated lines in a file
delete lines in the file
delete lines in the file   I am using a file as Input in program...The file contains round about 1,00,000 lines .My program after each iteration takes a data from that file at process it. My Question is when a particular data
Advertisements
delete lines in the file
delete lines in the file   I am using a file as Input in program...The file contains round about 1,00,000 lines .My program after each iteration takes a data from that file at process it. My Question is when a particular data
delete lines in the file
delete lines in the file   I am using a file as Input in program...The file contains round about 1,00,000 lines .My program after each iteration takes a data from that file at process it. My Question is when a particular data
Search and return lines of text file
Search and return lines of text file  I wrote the following code, that found lines of txt file with contains certain word, but it returns only the first or the last line! (However, the System.out.println show me all the lines
Java file number of lines
Java file number of lines In this section, you will learn how to count the number of lines from the given file. Description of code: Java has provide... the lines from the file. You can see in the given example, we have created
Java read lines from file
Java read lines from file  Any code example related to Java read lines from file? In my project there is requirement of reading the file line by line... of reading file line by line in Java. Can any one share me the code for reading
Read Lines from text file
need to do this for a file with 11 lines. Here's my code while((line...Read Lines from text file  Here's a brief desc of what my Java code does .. I'm using BufferedReader to read lines from a text files and split each
Count lines of a particular file
In this program you will learn how to read a file and count number of lines... and count no of lines in a file. Program first asks the user to enter.... Its reads the file(s) and then prints the no of lines in each file. Java
To dump 10lakh lines from a text file to a mysql table
To dump 10lakh lines from a text file to a mysql table  I need to dump a text file datas -100000 lines into a mysql table.i'm able to dump only 152000 lines only
in unix which command is used to sort the lines of data in a file in reverse order
in unix which command is used to sort the lines of data in a file in reverse order  in unix which command is used to sort the lines of data in a file... the lines of data in a file in reverse order
xml file display - XML
code to display the above xml file in tree structure where...xml file display   - - - - ADL SCORM CAM 1.3 - - session3 - Online Instructional Strategies that Affect Learner
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text File  I need to read a file that was selected by the user using... to display the names and path on the console. But I can not get the file to read line
svg file reading and display
svg file reading and display  i want a sample program for reading and displaying the content of a svg file
How to display a file for 24 hours?
How to display a file for 24 hours?  How to display a file for 24 hours
display co-occurrence words in a file
display co-occurrence words in a file  how to write java program for counting co occurred words in the file
display co-occurrence words in a file
display co-occurrence words in a file  how to write java program for counting co occurred words in the file
how to display pdf file on browser
how to display pdf file on browser   In my project i have created one pdf file(by pdfwriter) into my local mechine . after that it need to display in browser as a pdf file . but in jsp i can't display by iframe tag can anyone
how to sort the text file and display it in jtable
how to sort the text file and display it in jtable   my text file is: contig00001 length=586 numreads=4... ctgaggactcctcg i want to display the lines started with ">" symbol to be displayed
Java Display File-Extension
Java Display File-Extension       In this program you will learn how to display the file extension of the file. Here you will be asked to enter the file name
count the repeated character in one string
count the repeated character in one string  to find the how character occurrence in one String (for example the string is -java means there is 2 'a 'was repeated )   Hi Friend, Try the following code: import
display uploaded file option in editable form
display uploaded file option in editable form  how to show the uploaded file in editable mode on the jsp in struts2.0
display uploaded file option in editable form
display uploaded file option in editable form  how to show the uploaded file in editable mode on the jsp in struts2.0
how can i display a pdf file in a jtextarea
how can i display a pdf file in a jtextarea  I need to display a pdf file in a jtextfield or in a jtextarea.Atlest i need to displat it in a jframe.I have a button and while clicking on it ,i need to choose the pdf file and need
How to Display jrxml file on JSP page???
How to Display jrxml file on JSP page???  I made jrxml file using jasper report but how to display on jsp page?? i tried lot but still i didnt find out solution.so pls help me as soon as possible
Ajax Lines
Ajax Lines       A pretty extensive list of AJAX resources, tutorials, toolkits and websites. List of all useful and interested resources that may help for AJAX development. Collection
How to Remove Repeated Characters from the String?
How to Remove Repeated Characters from the String?  Hi, I trying to develop an application which will remove the repeated characters from... the example of how to remove repeated characters from the string. Please visit
how to display the output of the newly created excel file
how to display the output of the newly created excel file  the following program will create the test.xlsx file, how can i display the content of the test.xlsx file in the web browser? thanks, %@ page import
display error message for dupliacate record in properties file
display error message for dupliacate record in properties file  This is my code. Here if the properties file contain duplicate records(i.e.)more than one entry e.x.001,002,003,001 i want to display the error message "Duplicate
how to display the excel file content in the jsp
how to display the excel file content in the jsp  How to present the content of the newly created excel file in the following jsp to the web browser: print("code sample"); <%@ page import="java.io.InputStream" %>
Remove Repeated Characters from the String
Remove Repeated Characters from the String In this we are going to remove repeated characters from the string. For this, we have allowed the user to enter...}(?:(?=\\1).))" method that have removed all the repeated characters. Here
 

 

 

DMCA.com