1. Member
Join Date
Oct 2010
Posts
38
Rep Power
0

## 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.

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:
Java 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.

2. Member
Join Date
Oct 2011
Posts
83
Rep Power
0

## 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:
Java 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:
Java 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.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,547
Rep Power
11

## Re: Printing Distinct Numbers

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.

4. Member
Join Date
Oct 2010
Posts
38
Rep Power
0

## 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:

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.
Last edited by son012189; 10-19-2011 at 11:49 PM.

5. Senior Member
Join Date
Jan 2011
Location
Posts
227
Rep Power
3

## Re: Printing Distinct Numbers

That is already explaned to you
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).

6. Member
Join Date
Oct 2010
Posts
38
Rep Power
0

## 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.

Java 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]+" ");
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•