I can't figure out what am I doing wrong!! please help me before I lose it!
I'm trying to send 2 different strings to a function that will put each word in the string into a MAP.
string 1: "I CAR Collision Repair Training"
string 2: "Autocar India"
my map should be like this:
i
car
collision
repair
training
autocar
india
BUT when I print my map what I get is...well u can see at the end of my post the output :'(
it prints the last word of each string, and many times..I wrote an IF statement to not put duplicate words in the MAP and still I don't know what the hell is going wrong with my code!
here is the code (I added many printouts to see the serults at each stage)
class...{
Map <String,Term> bagOfWords=new HashMap <String,Term>();
public void makeDictinary(String str,int i){
Term term=new Term();
str=str.toLowerCase();
StringTokenizer token = new StringTokenizer(str);
while (token.hasMoreTokens()){
str = token.nextToken();
System.out.println("token str: "+str);
if(bagOfWords.containsKey(str))
{
System.out.println("this str exists: "+str);
bagOfWords.get(str).total++;
System.out.println("check IF");
for (Object key: bagOfWords.keySet()) {
System.out.println("in the bag: "+bagOfWords.get(key).term);
}
}
else
{
System.out.println("this str doesn't exists: "+str);
term.term=str;
term.total++;
System.out.println("this str was added: "+str);
bagOfWords.put(str,term);
System.out.println("check ELSE");
for (Object key: bagOfWords.keySet()) {
System.out.println("in the bag: "+bagOfWords.get(key).term);
}
}//end of else
}//end of while
System.out.println("check");
for (Object key: bagOfWords.keySet()) {
System.out.println("FINAL in bag of words: "+bagOfWords.get(key).term); }
}
}//end of class
the output:
token str: i
this str doesn't exists: i
this str was added: i
check ELSE
in the bag: i
token str: car
this str doesn't exists: car
this str was added: car
check ELSE
in the bag: car
in the bag: car
token str: collision
this str doesn't exists: collision
this str was added: collision
check ELSE
in the bag: collision
in the bag: collision
in the bag: collision
token str: repair
this str doesn't exists: repair
this str was added: repair
check ELSE
in the bag: repair
in the bag: repair
in the bag: repair
in the bag: repair
token str: training
this str doesn't exists: training
this str was added: training
check ELSE
in the bag: training
in the bag: training
in the bag: training
in the bag: training
in the bag: training
check
FINAL in bag of words: training
FINAL in bag of words: training
FINAL in bag of words: training
FINAL in bag of words: training
FINAL in bag of words: training
token str: autocar
this str doesn't exists: autocar
this str was added: autocar
check ELSE
in the bag: training
in the bag: training
in the bag: autocar
in the bag: training
in the bag: training
in the bag: training
token str: india
this str doesn't exists: india
this str was added: india
check ELSE
in the bag: training
in the bag: training
in the bag: india
in the bag: india
in the bag: training
in the bag: training
in the bag: training
check
FINAL in bag of words: training
FINAL in bag of words: training
FINAL in bag of words: india
FINAL in bag of words: india
FINAL in bag of words: training
FINAL in bag of words: training
FINAL in bag of words: training