0

EDIT: I just changed the TreeMap to HashMap and now it works a litttle bit but still doesn't do what I need it to do..

Hey,this is something I have been stuck on for a really long time.

My goal is to take text from a file sequence.txt and then call a constructor that passes a scanner s and an integer n. Inside of the constructor my goal is to read n words from sequence and save that in the ArrayList, and then the inner map is supposed to be able to take the word immediately following that sequence of n words and keep count of how many times n + thisWord exists.

So, an example would be these n (or 5) words:

so an to me for

And then the sixth could be and, so in the original map it would be:

map.put(arrList, innerMap);

where arrList would be contain so, an, to, me, for and the innerMap would look like this and | 1

And then from there on each new one would be added and for the same sequences it would increment the integer by one. I would be worried about the second part, but I can't even get my code to work, and I don't know what I've done wrong. Here is my constructor:

public RandomWriter(Scanner s, int n) {
        Map<String, Integer> valMap = new HashMap<String, Integer>();
        this.s = s;
        this.n = n;
        map = new HashMap<>(); `

        Queue<String> tmp = new LinkedList<String>();
        for (int i = 0; i < n; i++) {
            tmp.add(s.next());
        }

        while (s.hasNext()) {
            ArrayList<String> tmpList = new ArrayList<String>();
            tmpList.addAll(tmp);
            String current = s.next();

            if (!valMap.containsKey(current)) {
                valMap.put(current, 1);

            } else {
                valMap.put(current, valMap.get(current + 1));
            }

            tmp.add(current);
            tmp.remove();
            map.put(tmpList, valMap);
        }

       // s.next();
        System.out.println(map.keySet());
    }

Upon changing to HashMap it now prints out like this:

[[today, day], [me, and], [so, if], [in, for], [for, you], [top, in], [sent, receive], [for, many], [on, top], [or, on], [do, do], [side, so], [so, when], [under, on], [many, much], [to, do], [row, on], [for, when], [when, become], [dog, house], [toy, your], [many, is], [won, do], [become, inside], [why, to], [to, spell], [me, you], [school, under], [too, your], [hit, high], [can, do], [is, to], [for, how], [receive, get], [on, me], [animal, your], [to, animal], [inside, to], [your, for], [cat, many], [who, me], [spell, word], [do, cat], [house, live], [how, can], [your, who], [word, sent], [to, today], [canvas, word], [on, for], [low, ball], [which, many], [animal, if], [much, mouse], [live, living], [your, sent], [if, how], [high, kill], [ball, hit], [if, so], [in, side], [get, which], [you, if], [so, for], [do, if], [get, toy], [so, row], [if, to], [to, if], [win, won], [how, when], [inside, in], [dog, squirrel], [you, to], [and, you], [so, or], [word, school], [mouse, for], [for, why], [day, dog], [squirrel, fish], [your, if], [cat, dog], [for, which], [to, when], [more, for], [to, your], [which, cat], [living, canvas], [kill, win], [you, low], [when, inside], [fish, animal], [do, so], [many, more], [to, too], [when, to]]

This is the map it is entering into:

private Map<ArrayList<String>, Map<String, Integer>> map;

1
  • It is unclear how you're getting the values for the "inner map" from your description. As to the error, it's because ArrayList is not comparable (and thus cannot be stored in a TreeSet) Commented Dec 9, 2014 at 2:40

1 Answer 1

1

This is wrong :

        if (!valMap.containsKey(current)) {
            valMap.put(current, 1);

        } else {
            valMap.put(current, valMap.get(current + 1));
        }

You probably want :

        if (!valMap.containsKey(current)) {
            valMap.put(current, 1);

        } else {
            valMap.put(current, valMap.get(current) + 1);
        }

Since if the map contains the key current, you want to add 1 to the value of that key and not overwrite it with the value of key current+1, which may not even exist.

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

8 Comments

Yep this is correct.. I don't really even know what my original problem is now though. I'll add output to OP
@brian I find it impossible to understand what your program is supposed to do.
thats probably why it doesn't do what I want it to do
I've added the code for the map that it is supposed to be representing if that helps you understand
@brian What is this map supposed to represent? Can you explain that? I don't understand how the first n words you read are related to the rest of the words, and why you are using a list as the key to a map.
|

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.