Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By DiamondSoul

Thread: Printing Distinct Numbers

  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool 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. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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.
    son012189 likes this.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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. #4
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default 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-20-2011 at 12:49 AM.

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default 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. #6
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

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

Similar Threads

  1. MSAccess, rs.next() and SELECT DISTINCT
    By mine0926 in forum JDBC
    Replies: 13
    Last Post: 01-26-2011, 02:23 AM
  2. printing number of pos/neg numbers input?
    By shroomiin in forum New To Java
    Replies: 12
    Last Post: 09-25-2009, 03:15 AM
  3. Printing ranges of numbers using if/else statements
    By russbuss106 in forum New To Java
    Replies: 8
    Last Post: 03-05-2009, 06:03 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  5. Printing Fibonacci Numbers
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:43 PM

Posting Permissions

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