Here I’m trying to read a text file which contains only integers.in every line.For eg:
1
2
3
1
I wrote the following code to read the text file. Code as shown below.
package fileread;
import java.io.*;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
try{
FileInputStream fstream=new FileInputStream("C:/Users/kiran/Desktop/text.txt");
DataInputStream in=new DataInputStream (fstream);
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
in.close();
}
catch(Exception e){
System.err.println(e);
}
}
}
Now I want to retrieve only those integers which repeated and display it to the user.
In this case i want to display “1”.
How can I implement this in Java??
Note: Make sure your input doesn’t have trailing whitespace on each line. Also, note that when the list gets long, this implementation is not particularly memory friendly.