0

I'm trying to use a Scanner to read in a text file with fraction Strings of the form "a/b", store them in an array, then print to the console each unique one along with its count. I can print each separate entry in the text file with an accurate count, but I can't figure out how to print the contents of the array without any duplicates.

I'm also trying to accomplish this entirely within main. Here's what I've got so far:

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

        ArrayList<String> fracs = new ArrayList<String>();
        Scanner input = new Scanner(new File("input.txt"));
        ArrayList<String> fracsDisplay = new ArrayList<String>();

        while(input.hasNext())
            fracs.add(input.next());

        for(int i = 0; i < fracs.size(); i++)
        {
            int count = 0;
            for(int j = 0; j < fracs.size(); j++)
            {
                if(fracs.get(i).equals(fracs.get(j)))
                    count++;
            }
            System.out.println(fracs.get(i) + ": " + count);
        }

        input.close();
}
1
  • hmm, you could before adding the elements check for fracs.contains(...) if the array contains already an element and if so just increment the counter or avoid adding the element again. A Set is preventing duplicate entries by default. And why don't you use a Map if you want to print the entry-name as well as its count? Lookup the count (=value) via the unique entry (=key) Commented Jan 12, 2014 at 2:21

4 Answers 4

1

The easiest way to get rid of duplicates would be to use a Set, as you are storing Strings duplicates won't be added.

Instead of ArrayList<String> fracsDisplay = new ArrayList<String>(); you could use:

Set<String> fracsDisplay = new HashSet<>();
fracsDisplay.addAll(fracs);
for (String s : fracsDisplay) {
    System.out.println(s);
}

if some kind of order needs to be maintained then I would suggest a TreeSet, but either will handle the removal of duplicates... which seems to be what you want in this case.

Sign up to request clarification or add additional context in comments.

2 Comments

disadvantage if the entries should be available in an ordered structure
True, in this case a TreeSet could be used, OP didn't state whether order was important or not... answer updated to reflect your suggestion!
0

If a grasp it, this task is a "Word Count"-like task. To do such a task, HashMap is the best choice. I suggest you use a HashMap to do this task. Your algorithm has a O(n^2) time complexity. You can use a HashMap to reduce it down to O(n). Using a HashMap also helps you deduplicate. Hava a try.

        HashMap<String, Integer> fracs = new HashMap<String, Integer>();
        Scanner input = new Scanner(new File("input.txt"));

        while(input.hasNext()) {
            String frac = input.next();
            if (! fracs.containsKey(frac)) {
                fracs.put(frac, 1);
            } else {
                fracs.put(frac, fracs.get(frac) + 1);
            }
        }

        input.close();

        for (Map.Entry<String, Integer> fracCount : fracs.entrySet()) {
            System.out.println(fracCount.getKey() + " " + fracCount.getValue());
        }

If fraction strings should keep the order of occurance, use LinkedHashMap; If fraction String should keep the lexicographical order, or some spercific order, use SortedHashMap. Here I suggest you read "Java Generics and Collections" which focus on Java Collection framework.

Comments

0

Should you store everything into an ArrayList? You could use either a HashSet or a TreeSet since dublications there are not allowed.

Comments

0

You can use HashSet as Set collection will not allow duplicate values. If your order of array is important then you can use LinkedHashSet.

Set<T> set = new HashSet<T>();
Collections.addAll(set, array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.