# Printing Distinct Numbers

Printable View

• 10-19-2011, 08:18 PM
son012189
Printing Distinct Numbers
Write a program that reads in ten numbers and displays distinct numbers (i.e., if a number appears multiple times, it is displayed only once). Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it. After the input, the array contains only the distinct numbers.

Quote:

Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 1 2 3 6 4 5
Here is my attempt:
Code:

```import java.util.*; public class PrintingDistinctNumbers {         public static void main(String[] args) {                 Scanner input = new Scanner(System.in);                 System.out.print("Enter ten numbers: ");                 int num1 = input.nextInt();                 int num2 = input.nextInt();                 int num3 = input.nextInt();                 int num4 = input.nextInt();                 int num5 = input.nextInt();                 int num6 = input.nextInt();                 int num7 = input.nextInt();                 int num8 = input.nextInt();                 int num9 = input.nextInt();                 int num10 = input.nextInt();                 int[] values = {num1, num2, num3, num4, num5, num6, num7, num8, num9, num10};                 int array = distinctnumbers(values);                 System.out.println("The distinct numbers are: " + array);         }         public static void distinctnumbers (int[] values) {                          for (int i = values.length - 1; i >= 1; i --) {                         int currentNum = values[0];                         for (int j = 1; j <= i; j++) {                                 if (currentNum != values[j]) {                                         currentNum = values[j];                                 }                                }                 }         } }```
My code will not compile. I don't know why. Other than that I believe my code should work.
• 10-19-2011, 08:38 PM
DiamondSoul
Re: Printing Distinct Numbers
Okay, there are several things wrong with this program.

Firstly, the reason your code will not compile is that the method distinctnumbers() has no return value, so it doesn't make sense to store it into an int variable called array (as you are attempting to do in line 18).

Now, even if you fixed this, I'm pretty sure the program would not give the correct output. I would suggest that, rather than having a method distinctnumbers() which is called at the end of the program to remove duplicate numbers, you should do the removal of duplicates as the numbers are being inputted. More on this later.

Your method of inputting is very inefficient, coding wise. If you had 1000 inputs instead of 10, would you want to copy and paste that one input line 1000 times? Rather than have 10 separate variables which are stored in the array after the user gives input, just store the user's input directly into the array, like so:
Code:

```Scanner input=new Scanner(System.in); System.out.print("Enter ten numbers:"); int[] values=new int[10]; for(int i=0;i<10;i++) values[i]=input.nextInt();```
Now, I claim that you can remove the duplicate numbers inside the for loop, rather than after it. So your finished main method would look like this:
Code:

```Scanner input=new Scanner(System.in); System.out.println("Enter ten numbers:"); int[] values=new int[10]; int qty=0; //keep track of the number of distinct numbers for(int i=0;i<10;i++){     int num=input.nextInt();     boolean distinct=true;     //insert some code here to determine if the current number is being inputted for the first time     if(distinct) values[qty++]=num; } for(int i=0;i<qty;i++) System.out.print(values[i]+" ");```
I'll leave you to figure out what code can be used to actually determine if the number is distinct.
• 10-19-2011, 08:43 PM
pbrockway2
Re: Printing Distinct Numbers
Quote:

My code will not compile. I don't know why.
The compiler messages say why. If you can't understand a message, post it here and say which lines of your code it is referring to.

-----

What is your distinctnumbers() method supposed to do? I ask because, at the moment, it creates a new variable currentNum, assigns things to it and then returns. Viewed from the outside (where currentNum is not visible) it will not do anything at all.
• 10-19-2011, 11:44 PM
son012189
Re: Printing Distinct Numbers
@DiamondSoul Thanks, I will try that and post my fixed code later today.

@pbrockway2 I used ideone to compile my program and got this message:

Quote:

Main.java:11: incompatible types
found : void
required: int
int array = distinctnumbers(values);
^
1 error
My distinctnumbers() method is suppose to remove repeat numbers from an array.
• 10-20-2011, 12:02 AM
milovan
Re: Printing Distinct Numbers
That is already explaned to you
Quote:

Firstly, the reason your code will not compile is that the method distinctnumbers() has no return value, so it doesn't make sense to store it into an int variable called array (as you are attempting to do in line 18).

• 10-20-2011, 08:29 AM
son012189
Re: Printing Distinct Numbers
@milovan Huh? Sorry but I don't know what that has to do with my last reply :^):

@Diamond Soul I finally got it to work. Here's the finished code. Thanks.

Code:

```import java.util.*; public class PrintingDistinctNumbers {         public static void main(String[] args) {                 Scanner input = new Scanner(System.in);                 System.out.print("Enter ten numbers: ");                                int[] values = new int[10];                 int qty = 0;                 for(int i = 0; i < 10; i++) {                         int num = input.nextInt();                         boolean distinct = true;                         for(int j = 0; j < qty; j++) {                                 if (num == values[j]) {                                         distinct = false;                                         break;                                 }                         }                         if (distinct) values[qty++] = num;                 }                 System.out.println("The distinct numbers are: ");                 for(int i = 0 ; i < qty; i++) System.out.print(values[i]+" ");                } }```