# disticnt numbers

• November 15th, 2010, 10:55 PM
Fordy252
disticnt numbers
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..

Code Java:

```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; readNumbers();   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] + " "); } }   private static void readNumbers() {   Scanner sin = new Scanner(System.in); System.out.println("Enter" + SIZE + " numbers : "); for (int i = 0; i < numbers.length; i++) {   numbers[i] = sin.nextInt(); } } }```
• November 16th, 2010, 12:03 PM
DavidFongs
Re: disticnt numbers
First off, even if the logic was correct, the syntax of your else is wrong, since you are missing the brackets (without the brackets only the first line after the else will be executed).

You can accomplish what you want to do by looping over all the numbers and determining if the number is already in the distinct array.... and then if it isn't, outside of the inner for loop (since you have now checked all of the numbers in distinct) you can then add it to the distinct array.

You could also create a Set, add all the numbers, and then call toArray.... This will guarantee you have no duplicates in the array. Link: Set (Java Platform SE 6)
• November 18th, 2010, 06:50 PM
Fordy252
Re: disticnt numbers
Thanks for your help.. but i've being still trying to do this and i just can't get it working.. i am looping over the numbers entered array and trying to enter them to the distinct array only once but this only works for the first number scanned and not the rest... could someone please show me the code as to how its done .. it's probably just something obvious i am missing.. thank you.
• November 18th, 2010, 07:21 PM
copeg
Re: disticnt numbers
Unless you have restrictions, its much simpler to just use a set as DavidFongs suggests above. See his link, as well as How to Use Sets
• November 18th, 2010, 09:03 PM
Fordy252
Re: disticnt numbers
We are not allowed to use sets.. we were told to use only nested loops.. i am new to java and am still finding it very hard to get my head around nested loops.. this program is making me crazy.. i need to get it working.. thanks for advice.
• November 19th, 2010, 02:48 PM
DavidFongs
Re: disticnt numbers
Here is some psuedo code to do it with nested loops

Code :

```for(every number in the numbers array){ boolean found = false; for(every number (call it distinct) in the distinct array){ if(number is equal to distinct){ found = true; break; } } if(!found){ add number to the distinct array } }```
• November 21st, 2010, 10:40 AM
Fordy252
Re: disticnt numbers
Thanks very much David.. i finally got it working.. i knew it would be something simple i was not doing correct which was looping through the complete array first without taking any action.. works great now cheers..