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 6 of 6

Thread: Help PLZ .. =(

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help PLZ .. =(

    Hi All ..

    I need help with this Program .. The question is :

    Use a one-dimensional array to solve the following problem:Write an application that inputs five numbers, each of which is between 10 and 100, inclusive. As each number is read, display it only if it is not a duplicate of a number already read. Provide for the “worst case,” in which all five numbers are different. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs each new value.

    and this is the code I wrote :

    First Class containing main :

    public class UniqueTest
    {
       public static void main( String[] args )
         {
           Unique obj = new Unique();
           obj.getNumbers();
         } 
    }

    Second Class :

    import java.util.Scanner;
     
    public class Unique
     {
     
         public void getNumbers()
         {
            Scanner input = new Scanner( System.in );
            int [] array = new int [5];
     
            int count = 0; // number of uniques read
            int entered = 0; // number of entered numbers
            int num =0;
     
           while( entered < array.length )
            {
               System.out.print( "Enter number: " );
                num = input.nextInt(); 
     
                if ((num>=10) && ( num<=100))
                 {
                    boolean containsNumber = false;
                    entered++;
     
                     for (int i=0;i<array.length;i++)
                       {
             	 if (array[i]==num)
             	   containsNumber=true;
                       }
     
                     if ( !containsNumber )
                      { 
                         for (int i=0; i<array.length;i++)
                           {
                              array[i]=num;
                              count++;
                           }
     
                       } // end if
     
                     else
                      System.out.printf( "%d has already been entered\n",num );
                   } // end if
     
                    else
              System.out.println( "number must be between 10 and 100" );
     
             //Print the contents of the Array :
              for(int i=0 ; i<array.length;i++)
               {
                System.out.printf("%d\n",array[i]);
                 break;
               }
     
           } // end while
        } // end method getNumbers
     } // end class Unique

    The problem is that when I enter new unique number after the first number ,it will remove the first number and print only the second number .. This is the OutPut :

    Enter number: 11
    11
    Enter number: 27
    27
    Enter number: 80
    80
    Enter number: 100
    100
    Enter number: 60
    60

    and I want the OutPut to be like this :

    Enter number: 11
    11
    Enter number: 27
    11  27
    Enter number: 80
    11  27  80
    Enter number: 100
    11 27  80  100
    Enter number: 60
    11  27  80  100 60

    Any Help is Greatly appreciated ..


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help PLZ .. =(

    Hello Yazan.

    Welcome to the Java Programming Forums

    I have made some changes to your code. The problem was you were filling the entire array with the same num value.

    Take a look at this. I have bolded all the changes.

    import java.util.Scanner;
     
    public class Unique {
     
    	public void getNumbers() {
    		Scanner input = new Scanner(System.in);
    		int[] array = new int[5];
     
    		int count = 0; // number of uniques read
    		int entered = 0; // number of entered numbers
    		int num = 0;
     
    		[B]System.out.println("Enter number: ");[/B]
     
    		while (entered < array.length) {
     
    			num = input.nextInt();
     
    			if ((num >= 10) && (num <= 100)) {
    				boolean containsNumber = false;
    				entered++;
     
    				for (int i = 0; i < array.length; i++) {
    					if (array[i] == num)
    						containsNumber = true;
    				}
     
    				if (!containsNumber) {
     
    					[B]array[count] = num;[/B]
    [B]					//for (int i = 0; i < array.length; i++) {
    					//	array[i] = num;
    						count++;
    					//}[/B]
     
    				} // end if
     
    				else
    					System.out.printf("%d has already been entered\n", num);
    			} // end if
     
    			else
    				System.out.println("number must be between 10 and 100");
     
    			// Print the contents of the Array :
    			for (int i = 0; i < entered; i++) {
     
    	[B]			System.out.print(array[i] + " ");
    				//break;[/B]
     
    			}
     
    			[B]System.out.println("\n" + "Enter number: ");[/B]
     
    		} // end while
    	} // end method getNumbers
    } // end class Unique
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help PLZ .. =(

    Hi JavaPF..

    THX ALOT .. I really appreciate your help ..

    I run the program .. but I faced another Problem :

    The Code after modification :

    import java.util.Scanner;
     
    public class Unique {
     
    public void getNumbers() {
      Scanner input = new Scanner(System.in);
    	int[] array = new int[5];
     
    	int count = 0; // number of uniques read
    	int entered = 0; // number of entered numbers
    	int num = 0;
     
    	while (entered < array.length) 
    		{
    		System.out.println("Enter number: ");
    		num = input.nextInt();
     
    		if ((num >= 10) && (num <= 100)) {
    			boolean containsNumber = false;
    			entered++;
     
    			for (int i = 0; i < array.length; i++) 
    			 {   if (array[i] == num)		
    			     containsNumber = true;
    			 }	
     
    			if (!containsNumber) {
     
    			array[count] = num;
     
    			count++;
     
    			} // end if
     
    			else
     
    			System.out.printf("%d has already been entered\n", num);
     
    	     	      } // end if
     
    		  else
    		   System.out.println("number must be between 10 and 100");
     
    		// Print the contents of the Array :
    		for (int i =0;  i< entered; i++) {
     
    		System.out.print(array[i] + " ");
    		}
     
                                      System.out.println();
    	} // end while
                } // end method getNumbers
    } // end class Unique

    The OutPut is :
    Enter number: 
    30
    30 
    Enter number: 
    40
    30 40 
    Enter number: 
    30
    30 has already been entered
    30 40 0 
    Enter number: 
    50
    30 40 50 0 
    Enter number: 
    20
    30 40 50 20 0

    The Problem is value (0) .. How can I prevent it .. ??

    The OutPut Should be :
    Enter number: 
    30
    30 
    Enter number: 
    40
    30 40 
    Enter number: 
    30
    30 has already been entered
    30 40  
    Enter number: 
    50
    30 40 50  
    Enter number: 
    20
    30 40 50 20


    Please tell me if you know the answer ..

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help PLZ .. =(

    No problem

    Try changing:

    int[] array = new int[5];

    to

    int[] array = new int[4];
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5

    Default Re: Help PLZ .. =(

    I think it'd be easier for you to add the numbers to string
    String thing += num + " "
    and then parse it
    int ln = 0;
    for(int i=0;i<thing.length()) {
     if(this.substring(i,i) == " ") {
      System.out.printIn(this.substring(ln,i-1)
      ln = i+1;
     }
    }
    . But that's just my opinion. Everybody has their own.

    EDIT: Oops. Sorry. You had to do it an Array.

  6. #6
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: Help PLZ .. =(

    I'm a newbie but I think I figured it out:
    public class DuplicateEliminator {
     
       public void getNums(){
            Scanner input=new Scanner(System.in);
    	int[]digits=new int[4];
            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 isDup=false;
                        entered++;
     
                        for (int i=0;i<digits.length;i++) {
                                if (digits[i]==num)
    			     isDup=true;
                                 entered--;//do not add duplicate digits to array
                                 }
     
                                if (!isDup) {
                                    digits[count]=num;
    			        count++;
                                  	}
                   else
                        System.out.printf("%d is a duplicate number\n", num);
     
                   for(int i=0;i<count;i++) {
                        System.out.printf(digits[i] + " ");
                        }
    	}
          }
        }
     }