Is this good code? I do believe I improved quite a lot since last year, still I would really appriciate some feedback :)
import javax.swing.JOptionPane;
import java.util.ArrayList ;
public class IfNotRepeated {
/*
* 7.12 (Duplicate Elimination) Use a one-dimensional array to solve the following problem:
Write an application that inputs five numbers, each between 10 and 100, inclusive. As each number
is read, display it only if it’s 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 enters each new value.
*/
final static int ELEMENTS = 5 ;
final static int UPPER_BOUND = 100 ;
final static int LOWER_BOUND= 10 ;
public static void main(String[] args) {
int number = 0; //user's input
while ( true ) //allows the input of numbers which are bounded between 10 and 100 encompass,
//whenever this boundary is violated, the program ends, without showing any information.
{
ArrayList <Integer> vector = new ArrayList <Integer> (5) ; //Array of not repeated user inputs', I need to get a new one
//every time the user had the chance to input ELEMENTS numbers
for ( int i = 0 ;
i < ELEMENTS
&& ( number = gimmeValue() ) >= LOWER_BOUND
&& number <= UPPER_BOUND
; i++ ) // iterates as long as there are less than ELEMENTS numbers per array, and the boundary is respected
{
if ( !vector.contains(number) )
vector.add(number) ; //adds a number if the legit value entered hasn't been previously entered (in this serie).
String elements = "" ;
for ( int notRepeatedElement : vector )
elements += String.valueOf(notRepeatedElement) + '\n' ; //concatenates not repeated elements leaving a new line at the end
JOptionPane.showMessageDialog(null, elements); //show every non-repeated element
}
if ( number < LOWER_BOUND || number > UPPER_BOUND )
break ; // if out bounds, exit program
}
}
public static int gimmeValue () //shows an input dialog for the user to enter a number and returns that value as an Int
{
String number = JOptionPane.showInputDialog("Enter a number between 10 and 100 encompass:") ;
return Integer.valueOf(number) ;
}
}