ok so the program gets a list of numbers, and it counts how many times a number is entered.

for(int i = 0; i < numList.size(); i++){
		for(int j = numList.size() - 1; j > i; j--){
			if(numList.get(i) == numList.get(j)){
				count++;
				numList.remove(j);
				
			}
		}
		System.out.println(numList.get(i) +"  " + count);
		count = 1;
	}

this works except if i were to enter a number that's 100 or greater, it doesn't work which is weird.

for example, if i entered: 23, 23, 5, 3.
it would output:
23 2
5 1
3 1
this is correct

this doesn't work if i were to enter 100 or 500 or anything greater than 100.
input: 500, 500, 2, 5.
output:
500 1
500 1
2 1
5 1

Recommended Answers

All 4 Replies

try posting the rest of your code.

ArrayList <Integer> numList = new ArrayList <Integer>();
	
	int num = 0;
	
	while(num != -999){
		System.out.println("enter a series of numbers or typed -999 to stop: ");
		num = kb.nextInt();
		if(num == -999){
			break;
		}
		numList.add(num);
	}

thanks for helping me out...

if(numList.get(i) == numList.get(j))

You are actually comparing if two Integer objects are referencing the same object. Try numList.get(i).intValue() == numList.get(j).intValue()

Couldn't say for sure why the way you are doing it works in some cases.

thanks!! that works!!!

also the equals method works too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.