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

This is a small summary of your code:

Term term = new Term();
while () {
    if (str exists) {
      // don't add
    } else {
      // add term
    }
}

The problem is that term is declared outside of the while. So you do new Term() once. You created only ONE instance. So when you put into the Map bagOfWords the 'term' you put the same term with each iteration.
Example:
The first time you add for example the 'i'. That is the value of the instance term.
At the second time, you put the 'car' like this:

term.term=str;
bagOfWords.put(str,term);

BUT that 'term' is the same instance that had the first 'i'. So since it is the same instance in the memory, you changed its value. The MAP had this inside:

'i', term='i'

When you did this at the second time: term.term='car', you change the value that the term has. But that term is the same as the one already in the map. So every time you do this term.term=str, you change the value of the term instance which is the same term set for all the keys in the MAP.

'i', term='car'
'car', term='car'

Then:

'i', term='collision'
'car', term='collision'
'collision', term='collision'


So every time you want to add a new term, then create a new instance:

public void makeDictinary(String str,int i){

// REMOVE THIS		
//Term term=new Term();

str=str.toLowerCase();

....

else
		        {
		             System.out.println("this str doesn't  exists: "+str);

// ADD HERE
Term term=new Term();

		            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);
}

...
}
This question has already been answered. Start a new discussion instead.