1

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. output.txt

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:

console_logging

The input is as follows:

input.txt

Someone please help me explaining why this is happening.

4
  • I've never tried to read an output file in the same application that's writing the file. I think you would have better luck reading the input file multiple times. Once for each number and multiple times to check for duplicate numbers. Commented Oct 19, 2024 at 12:23
  • 3
    Don't use images for text like the outputs. Paste the actual text. Commented Oct 19, 2024 at 12:25
  • fwiw you can use streams. Pass the input file and output file paths on the command line: import java.io.PrintWriter;import java.nio.file.Files;import java.nio.file.Path;import java.util.Scanner;public class Unique {public static void main(String[] args) throws Exception {try (Scanner input = new Scanner(Files.newBufferedReader(Path.of(args[0]))).useDelimiter("\\R");PrintWriter out = new PrintWriter(Files.newBufferedWriter(Path.of(args[1])))) {input.tokens().distinct().forEach(t -> out.println(t));}}} Commented Oct 19, 2024 at 12:28
  • @GilbertLeBlanc I know it is not recommended what I am doing in this program and it is verbose too, the simple approach is to use a HashSet to filter out duplicates but here it is just a constraint that we cannot use any data structure like array, arraylist, set, map, etc...we have to do it only using file IO. Commented Oct 19, 2024 at 12:33

1 Answer 1

1

The mistake is that I am not flushing the data from the stream to the file everytime I decide to write a number. So every time the data is in the stream and not getting written to the file as a result the Buffered reader is reading null everytime. I have to just add:

if(!doesExist(mobileNo)){
   writer.println(mobileNo);
   writer.flush()
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.