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