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 | 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;
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.