I am a beginner in java and trying to solve the below use case.
Use case : From a file(input.txt) having numbers, find out and write all the unique numbers to a different file(output.txt) without using any Data structures(array, set, map, etc…).
For the mentioned use case I wrote the following code in java:
import java.util.*;
import java.io.*;
class Merge{
public static boolean doesExist(String mobileNo) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("output.txt"));
String exist = reader.readLine();
//logging
System.out.println(exist + " " + mobileNo);
while(exist != null){
if(exist.equals(mobileNo)) return true;
exist = reader.readLine();
}
return false;
}
public static void main(String[] args) throws IOException{
PrintWriter writer = new PrintWriter("output.txt");
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
String mobileNo = reader.readLine();
while(mobileNo != null){
if(!doesExist(mobileNo)) writer.println(mobileNo);
mobileNo = reader.readLine();
}
writer.flush();
reader.close();
writer.close();
return ;
}
}
The above program is not filtering the unique numbers, the output picture is attached.
I tried to debugg using the console printing, and found that the output.txt is not getting read.
The following is the console output of logging:
The input is as follows:
Someone please help me explaining why this is happening.
You need to sign in to view this answers