Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Thanks in advance... The tiny.txt file produces UNIQUE (and a newline) to the sc

ID: 3702455 • Letter: T

Question

Thanks in advance...

The tiny.txt file produces UNIQUE (and a newline) to the screen because it contains no duplicate words

The big file produces NOT UNIQUE (and a newline) to the screen because it contains a duplicate word

You must:

-Must use a HashSET

- call only the absolute minimal number of optimal method(s) on the container to solve the problem.

Your program must use the command line and args[0] to read the input file.

tiny.txt

big.txt

Here my current code ( don't know why but keep on skping over the first test) : thanks in advance

import java.io.*;

import java.util.*;

public class Lab5 {

public static void main(String[] args) throws Exception {

HashSet<String> hset = new HashSet<String>();

BufferedReader reader = new BufferedReader(new FileReader(args[0]));

while (reader.ready())

{

if (!hset.add(reader.readLine()))

System.out.println("UNIQUE");

else

System.out.println("NOT UNIQUE");

}

}

}

Explanation / Answer

import java.io.*; import java.util.*; public class Lab5 { public static void main(String[] args) throws Exception { HashSet hset = new HashSet(); BufferedReader reader = new BufferedReader(new FileReader(args[0])); while (reader.ready()) { if (!hset.add(reader.readLine())) { System.out.println("NOT UNIQUE"); System.exit(0); } } System.out.println("UNIQUE"); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote