Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

problem with integer incrementation

807598Mar 8 2006 — edited Mar 8 2006
Hi,

I have method majority vote which initialises 3 integers at the start, then as it goes through it increments them. the problem is that each time the method is called the increments from the previous method were saved.
I really don't know why this is because i've initialised them to 0 at the beginning and also after the processing.

heres my code
public Float majorityVoteclass3(Vector v)
	{
		Float class1 = new Float(0.0);
		Float class2 = new Float(0.0);
		Float class3 = new Float(0.0);
		Float clas4 = new Float(4.0);
		Float greatestClass = new Float(0.0);
		int class1Count = 0;
		int class2Count = 0;
		int class3Count = 0;

		for(int i=0;i<KNN.size();i++)
		{
			classifier = (Vector)v.elementAt(i);
			classifierClass = (Float)classifier.elementAt(classifier.size()-1);
			if (classifierClass.floatValue() == 1)
			{
				class1 = classifierClass;
				class1Count ++;
			}
			else if(classifierClass.floatValue() == 2)
			{
				class2 = classifierClass;
				class2Count ++;
			}
			else
			{
				class3 = classifierClass;
				class3Count ++;
			}
		}
		System.out.println("calss 1 size" +class1Count);
		System.out.println("calss 2 size" +class2Count);
		System.out.println("calss 3 size" +class3Count);
		if((class1Count > class2Count) && (class1Count > class3Count))
		{
			class1Count = 0;
			class2Count = 0;
			class3Count = 0;
			return class1;
		}
		else if((class2Count > class1Count) && (class2Count > class3Count))
		{
			class1Count = 0;
			class2Count = 0;
			class3Count = 0;
			return class2;
		}
		else if((class3Count > class1Count) && (class3Count > class2Count))
		{
			class1Count = 0;
			class2Count = 0;
			class3Count = 0;
			return class3;
		}
		else
		{
			class1Count = 0;
			class2Count = 0;
			class3Count = 0;
			return clas4;
		}
	}
Can anybody help to expain why this would be?

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 5 2006
Added on Mar 8 2006
4 comments
32 views