Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Displaying the contents of an array.

    My code takes 5 user input numbers stores them in an array and removes any duplicates using a for loop for iteration, everything works fine but I'm wondering how to a) display the final array of user input numbers b) have an error message alert the user if the user inputs more numbers than the array allows?
    Code:
    	import java.util.Scanner;
    public class DuplicateNumbers {
    
    		public static void main(String[] args) {
    		
    			Scanner input=new Scanner(System.in);
    			int[]digits=new int[5];
    		        int count=0;
    			int entered=0;
    			int num;
    		 
    		        while (entered<digits.length) {
    		            System.out.print("Enter a number between 10 and 100: \n");
    		            	num = input.nextInt();
    					
    			    		if ((num>=10)&&(num<=100)) {
    		                    boolean ifDuplicate=false;
    		                    	entered++;
    		 
    			                    for (int i=0;i<digits.length;i++) {
    			                            if (digits[i]==num)
    			                            	ifDuplicate=true;
    			                             		entered--;
    			                             }
    		 
                        if (!ifDuplicate) {
                            digits[count]=num;
                            			count++;
                          	}
    				               else
    				                    System.out.printf("%d is a duplicate number please enter another number\n", num);
    		 
    								               for(int i=0;i<count;i++) {
    								                    System.out.printf(digits[i] + " ");
    								                    }
          }
    	}	    
      }
    }

  • #2
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    These modifications seem to do everything you're looking for:

    Code:
    import java.util.Scanner;
    public class DuplicateNumbers {
    
    		public static void main(String[] args) {
    		
    			Scanner input=new Scanner(System.in);
    			int[]digits=new int[5];
    		        int count=0;
    			int entered=0;
    			int num;
    		 
    		        while (entered<digits.length) {
    		            System.out.print("Enter a number between 10 and 100: \n");
    		            	num = input.nextInt();
    					
    			    		if ((num>=10)&&(num<=100)) {
    		                    boolean ifDuplicate=false;
    		 
    			                    for (int i=0;i<digits.length;i++) {
    			                            if (digits[i]==num)
    			                            	ifDuplicate=true;
    			                             }
    		 
                        if (!ifDuplicate) {
                            digits[count]=num;
                            			count++;
    											entered++;
                          	}
    				               else
    				                    System.out.printf("%d is a duplicate number please enter another number\n", num);
    		 
    								               
          }
    		
    	}	
    	for(int i=0;i<count;i++) {
         System.out.printf(digits[i] + " ");
         }    
      }
    }
    It stops asking for input after it has 5 distinct numbers, so you shouldn't need an error message.

  • Users who have thanked srossd for this post:

    dreadfate (02-23-2011)


  •  

    Posting Permissions

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