Skip to main content
2 of 3
fixed indentation of last class
newbie
  • 879
  • 8
  • 23

Simple Java array stores and shows not repeated elements entered by user

I made a new version of this which was actually not behaving correctly, because it showed duplicated values, it just didn't add them again, this is the link to that program, someone point at correctly I think that the problem didn't state that student should use an ArrayList, but an Array, so I made this program again with an array, and I think is a lot better now. I would love some feedback, is this good code? I'm following the advices I got in my last post, please check answers. It works very well now and it took me quite a few hours to get it to work properly, but I loved coding it and learnt quite a few things.


class TestUserInputDataHandler

import javax.swing.JOptionPane;

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

public class TestUserInputDataHandler {
    
    public static UserInputDataHandler userInputContainer; 
    
    public static void main(String[] args) {
        final int USER_INPUTS = 5 ;
        //create UserInputDataHandlerObject
        userInputContainer = new UserInputDataHandler() ;
        //ask user to enter five valid int values
        //everytime user inputs a new value, show non-duplicate ones
        for ( int i = 0 ; i < USER_INPUTS ; i++ )
        {
            //let user input data
            getUserInput() ;
            //print not duplicated values entered by user
            printNonDuplicates() ;
        }
    }
    
    public static void printNonDuplicates(){
        String nonDuplicatedValues = 
                userInputContainer.nonDuplicatedValuesWithFormat();
        JOptionPane.showMessageDialog(null, 
                String.format ("Nonduplicated values:\n%s",nonDuplicatedValues) );      
    }
    
    public static void getUserInput(){
        //ask user to enter a new value and store it in a variable
        int newUserInput = Integer.valueOf(JOptionPane.showInputDialog(null, 
                "Enter a value between 10 and 100 inclusive)")) ;
        //let the container handle the new user input
        userInputContainer.handleUserInput(newUserInput);
    }
}

class UserInputData

public class UserInputData {
    //userInput field:it must be between 10 and 100 inclusive
    int userInput ;
    //isDuplicated field:false ONLY if number was entered by user once
    boolean isDuplicated ;
    //constructor
    public UserInputData ( int input ) {
        //check if input is valid
        if ( inputIsValid ( input ) ){
            this.userInput = input ;
            this.isDuplicated = false ;
        }else{
            throw new IllegalArgumentException
            ( "Invalid user input (must be between 10 and 100 inclusive" ) ;
        }
    }
    //userInput getter
    public int getNumericData (){
        return userInput ;
    }
    //isDuplicated getter
    public boolean isDuplicated(){
        return this.isDuplicated;
    }
    
    
    public boolean inputIsValid ( int input ){
        //constants declaration
        final int UPPER_BOUND = 100 ;
        final int LOWER_BOUNDS = 10 ;
        //return boolean if data is valid or invalid
        if ( input >= LOWER_BOUNDS && input <= UPPER_BOUND ){
            return true ;
        }else{
            return false ;
        }
    }
    public void setAsDuplicated () {
        this.isDuplicated = true ;
    }
}

class UserInputDataHandler

public class UserInputDataHandler {
    //Array of not-duplicated valid user input
    UserInputData[] handler ;
    //constants
    final int USER_INPUTS = 5 ;
    //constructor
    public UserInputDataHandler(){
        this.handler = new UserInputData[USER_INPUTS] ;
    }
    //handles user's input 
    public void handleUserInput (int newInput) {
        int storagePos = 0 ;
        //check if number already exists in array
        if ( ( storagePos = numberAlreadyEntered (newInput ) ) >= 0 )  {
            //set contained UserInputData object as duplicated
            this.handler[storagePos].setAsDuplicated();
        }else{
            //create UserInputData object
            UserInputData userInput = new UserInputData(newInput) ;
            //store new UserInputData object @handler's storagePos
            //value needs to be converted to possitive
            this.handler[ Math.abs(storagePos + 1)] = userInput ;
        }
    }
    //find @storagePos where newInput should be stored,or set repeated input as 
    //duplicated
    public int numberAlreadyEntered (int newInput ){
        //variable to go over handler
        int storagePos = 0 ;
        //check if the value was already entered
        while 
            (   //handler[storagePos] is not null               
                this.handler[storagePos] != null &&
                //if handler[storagePos] doesn't have newInput's numerical value
                newInput != this.handler[storagePos].getNumericData() &&
                //if storagePos < USER_INPUTS
                storagePos < USER_INPUTS 
                    )
        {
            //check next handler storage position
            storagePos++ ; 
        }
        //the true side of the following if must return @storagePos, where a 
        //duplicate value was found
        if ( this.handler[storagePos] != null && storagePos != USER_INPUTS ){
             return storagePos ;
        }else{
            //return a negative number if null pointer or unreachable position 
            //@storagePos
            return ( -1 * storagePos ) - 1;
        }
    }
    //return non duplicate values
    public String nonDuplicatedValuesWithFormat(){
        //empty string
        String s = "" ;
        //concat string with nonduplicated values
        for ( int i = 0 ;
                this.handler[i] != null &&
                i < USER_INPUTS
                    ; i++   )
        {
            //concatenate nonduplicated values
            if ( ! this.handler[i].isDuplicated() ){
                s += this.handler[i].getNumericData() + "\n" ;
            }
        }
        //return concatenated String
        return s;
    }
    
}
newbie
  • 879
  • 8
  • 23