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"); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.