# Print Distinct Numbers help

• November 15th, 2010, 11:10 PM
Fordy252
Print Distinct Numbers help
Hi, i would really apreciate help with this one. I am to write a program that for the user to enter 10 numbers and it will print only distinct numbers.. in other words.. it will display the numbers entered but if the same number is entered more than once it will display them only once.. for example , if the user enters numbers 1,1,2,2,3,4,5,6,6,7.. the output should be.. 1,2,3,4,5,6,7. I have being trying for the last week to get this finished but no success. I have a loop that searches through the numbers entered array and checks each one if it is in the distinct array each time to try and enter the number only once into the disticnct array but it's not happening somehow. Below is my code.. i really apreciate any help.. thanks..

ps.. sorry.. i still don't know how to attach the code window to my thread.. how do i do this?

package ex2;

import java.util.Scanner;

public class DistinctNumbers {
static final int SIZE = 10;
static int[] numbers = new int[SIZE];
static int[] distinct = new int[SIZE];

public static void main(String[] args) {

int disNumCount =0;

for (int i = 0; i < numbers.length; i++) {
int num = numbers[i];

for (int x = 0; x < distinct.length; x++) {
if (num == distinct[x]){
System.out.println("found a number already entered " + num);
break;
}
else
System.out.println("found a new distinct number!!");
distinct[disNumCount] = num;
disNumCount++;

break;
}

}

System.out.println("Numbers Entered :");
for (int i = 0; i < SIZE; i++) {
System.out.print(numbers[i] + " ");
}

System.out.println();
System.out.println("Distinct Numbers : ");
for (int i = 0; i < SIZE; i++) {
System.out.print(distinct[i] + " ");
}
}