Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

i need a java program that reads a text file then makes sure the links in it are not…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-haiku-20241022’s answer.
  1. User

    i need a java program that reads a text file then makes sure the links in it are not duplicates

  2. Assistant

    Certainly! Below is a simple Java program that reads a text file containing links (one per line), removes duplicate links, and then prints the unique links. You can easily modify it to write the unique links back to a file if needed.

    import java.io.*;
    import java.nio.file.*;
    import java.util.*;
    
    public class RemoveDuplicateLinks {
        public static void main(String[] args) {
            if (args.length < 1) {
                System.out.println("Usage: java RemoveDuplicateLinks <input-file>");
                return;
            }
    
            String inputFile = args[0];
            Set<String> uniqueLinks = new LinkedHashSet<>(); // preserves insertion order
    
            try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputFile))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    line = line.trim();
                    if (!line.isEmpty()) {
                        uniqueLinks.add(line);
                    }
                }
            } catch (IOException e) {
                System.err.println("Error reading file: " + e.getMessage());
                return;
            }
    
            System.out.println("Unique links:");
            for (String link : uniqueLinks) {
                System.out.println(link);
            }
        }
    }
    

    How to use:

    1. Save this code in a file named RemoveDuplicateLinks.java.
    2. Compile it:
      javac RemoveDuplicateLinks.java
      
    3. Run it with your input file as an argument:
      java RemoveDuplicateLinks links.txt
      

    Explanation:

    • The program reads each line from the input file.
    • It trims whitespace and ignores empty lines.
    • It stores links in a LinkedHashSet to automatically remove duplicates while preserving the original order.
    • Finally, it prints the unique links.

    If you want me to help you modify this to write the unique links back to a file, just ask!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.