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