Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: disticnt numbers

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

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


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default 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)

  3. The Following User Says Thank You to DavidFongs For This Useful Post:

    Fordy252 (November 18th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  7. #6
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: disticnt numbers

    Here is some psuedo code to do it with nested loops

    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
        }
    }

  8. #7
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

Similar Threads

  1. help with a loop (pyramid of numbers)
    By ande6870 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 7th, 2010, 08:17 PM
  2. Help with sorting numbers
    By planktonx in forum Collections and Generics
    Replies: 2
    Last Post: October 5th, 2010, 08:01 AM
  3. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  4. Roman Numbers
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 12th, 2009, 02:19 AM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM