0

I am building a program which is supposed to tell if a list of words from a text file are all unique or if there is a duplicate using a HashSet. If there is a duplicate, the only output should be "NOT_UNIQUE", and if there are no duplicates, the output should be "UNIQUE".

I believe the way to do this is to add the words from the file to the HashSet, and if a word can't be added it is because that word is a duplicate. I have already started building the program, but the program outputs "UNIQUE" for every word as opposed to just once at the end of the program. I think this has to do with my use of a while-loop and infile.readLine(), but I'm not sure what else to do here. Thank you for your help. My code is below:

import java.io.*;
import java.util.*;

public class Lab5
{
    public static void main (String[] args) throws Exception
    {
        HashSet<String> set = new HashSet<String>();
        BufferedReader infile = new BufferedReader( new FileReader( args[0] ) );
        while ( infile.ready() ) 
        {
            String word = infile.readLine();
            if ( !set.add(word) )
                System.out.println("NOT_UNIQUE \n");
            else
                System.out.println("UNIQUE \n");
        }
    } 
}
user3481644
  • 398
  • 2
  • 12
A.J.
  • 97
  • 1
  • 8

2 Answers2

1

You are printing your UNIQUE or NOT_UNIQUE outputs in a loop while your BufferedReader is ready to be read, at each line read, so it'll print a value for each line that's been read.

You probably want to refactor your design here. The logic below applies only if you want to actually keep the whole of the data read from file, otherwise there will be more efficient solutions, (see Max Mammel's).

  • Read all the lines from the file
  • Store them in a List (allows duplicates)
  • When done reading, initialize a new Set passing your List in its constructor (that'll trim it into a set of unique Strings)
  • Then compare the sizes of both - if they're different, you have non-unique items
  • Off-topic: remember to close your streams!
Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106
  • Thank you for your help. – A.J. Oct 05 '16 at 15:48
  • as for the Off-topic, what does it mean to close my streams? – A.J. Oct 05 '16 at 15:50
  • @Alex [here](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#close%28%29)'s the basic API for `close` on `InputStream`s, and [here](http://stackoverflow.com/questions/9547843/do-i-need-to-close-an-inputstream-in-java)'s some SO literature about it. Either close it in a `finally` statement, or use Java 7's try-with-resources. – Mena Oct 05 '16 at 15:52
1

Flags aren't elegant, but in this case I think it's a pragmatic solution. You can set a flag and bail out of the loop if you don't want it to display for every word:

import java.io.*;
import java.util.*;

public class Lab5
{
    public static void main (String[] args) throws Exception
    {
        boolean isUnique = true;
        HashSet<String> set = new HashSet<String>();
        BufferedReader infile = new BufferedReader( new FileReader( args[0] ) );
        while ( infile.ready() ) 
        {
            String word = infile.readLine();
            if ( !set.add(word) ) {
                isUnique = false;
                break;
            }
        }

        System.out.println(isUnique ? "UNIQUE \n" : "NOT_UNIQUE \n");

    } 
}

Remember to close out your file descriptor if you put this into an application.

Max Mammel
  • 51
  • 2
  • While your answer is more efficient than Berger's - now deleted, it doesn't close the streams (note that OP didn't bother to either). – Mena Oct 05 '16 at 15:44