# Help PLZ .. =(

• May 1st, 2010, 08:21 AM
Yazan
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 :

Code :

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

Second Class :

Code :

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

Code :

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

Code :

```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 ..
• May 1st, 2010, 09:22 AM
JavaPF
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.

Code :

```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```
• May 1st, 2010, 12:54 PM
Yazan
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 :

Code :

```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 :
Code :

```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 :
Code :

```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 .. :confused:
• May 1st, 2010, 02:38 PM
JavaPF
Re: Help PLZ .. =(
No problem :)

Try changing:

Code :

`int[] array = new int[5];`

to

Code :

`int[] array = new int[4];`
• May 2nd, 2010, 03:27 PM
scribbleno1
Re: Help PLZ .. =(
I think it'd be easier for you to add the numbers to string
Code :

`String thing += num + " "`
and then parse it
Code :

```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.
• November 5th, 2010, 12:39 PM
wildand5150
Re: Help PLZ .. =(
I'm a newbie but I think I figured it out:
Code :

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