user2744515
user2744515

Reputation: 84

when i try to add to treeset it returns blank

i have an assignment for school and this is what i have so far with notes of what i'm trying to do

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

public class UniquesDupesTester
{
    public static void main( String args[] ) throws IOException
    {
        // make a Scanner and associate it with "UniquesDupes.dat"
        // as long as there are Strings in the file

            // read in a String,
            // create a UniquesDupes object with it
            // print the object

            Scanner in = new Scanner(new File("UniquesDupes.dat"));



            while (in.hasNextLine())
            {
                String n = in.nextLine();
                UniquesDupes a = new UniquesDupes(n);
                a.getUniques();
                a.getDupes();
                System.out.println (a);
            }




    }
}

seperate file

import java.util.Set;
import java.util.TreeSet;
import java.util.Arrays;
import java.util.ArrayList;

public class UniquesDupes
{
    private ArrayList<String> list;


    /**
     * constructs a UniquesDupes object such that list contains the space delimited strings
     * parsed from input
     * @param input a String containing the list of words separated by spaces
     */
    public UniquesDupes(String input)
    {
        list = new ArrayList<String>();

        String[] words = "abc cde fgh ijk".split(" ");
        ArrayList<String> list = new ArrayList<String>(Arrays.asList(words));
    }

    /**
     * returns a set of Strings containing each unique entry in the list
     */
    public Set<String> getUniques()
    {
        Set<String> uniques = new TreeSet<String>();

        for(String a:list)
        {
            uniques.add(a);
        }

        return uniques;
    }

    /**
     * returns a set of Strings containing each entry in the list that occurs more than once
     */
    public Set<String> getDupes()
    {
        Set<String> uniques = new TreeSet<String>();
        Set<String> dupes = new TreeSet<String>();

        for(String a:list)
        {
            uniques.add(a);
        {
            if(uniques.add(a) == false)
            {
                dupes.add(a);
            }
        }
        }


        return dupes;
    }

    /**
     * returns the original list, the list of duplicates and the list of uniques
     * @return the String version of the object
     */
    public String toString()
    {
        return "Orig list :: " + list
              + "\nDuplicates :: " + getDupes()
              + "\nUniques :: " + getUniques() + "\n\n";
    }
}

here is the dat file if you need it

a b c d e f g h a b c d e f g h i j k
one two three one two three six seven one two
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6

it compiles and runs but all the files return blank i have no idea what i did wrong help or hints would be appreicated

Upvotes: 0

Views: 177

Answers (1)

ppeterka
ppeterka

Reputation: 20726

The logic is almost correct.

The UniquesDupes class is almost OK, but the constructor is not OK. It should just be

public UniquesDupes() // no args, default constructor
{
    list = new ArrayList<String>(); //just initialize the list
}

At the same time, said class would need an addString method:

public void addString(String input) {
    String[] words = input.trim().split(" "); //split the input string into words by spaces, after trimming whitespace
    this.list.addAll(Arrays.asList(words)); //adding them to the list.
}

The while loop should be changed a bit. You want only 1 instance of the UniquesDupes class, and then add each line using the addString method created before.

       UniquesDupes a = new UniquesDupes(); //create it here
       while (in.hasNextLine())
        {
            String n = in.nextLine();
            a.addString(n); //adding the string
        }

Then the results need to be handled differently

            Collection<String> uniques = a.getUniques();
            Collection<String> dupes = a.getDupes();

            System.out.println (uniques.toString());
            System.out.println (dupes.toString());

That said, the logic was almost right...

An ugly thing you did is however this part:

    list = new ArrayList<String>(); //using instance variable

    String[] words = "abc cde fgh ijk".split(" ");
    ArrayList<String> list = new ArrayList<String>(Arrays.asList(words));
    // ^^ declaring local variable the same name as the instance variable 

This is bad. You shouldn't do it. No-no. Don't do it again! Picking up this habit makes code exceptionally hard to read, and crazy insane to maintain...

Upvotes: 1

Related Questions