javaarraysjavafxoverwrite

Avoid overwriting randomly generated index in an array


How do you avoid overwriting an array if a number is randomly generated twice?

I am creating a simple javaFX program where a user can create or search for an account. The account details need to be stored at a random position within the array. My biggest challenge is not knowing how to prevent my program from overwriting an element at an index position that has already been filled.

   SecureRandom rand = new SecureRandom();
   int randomNum;
   String label;
    
    
   String [] name = new String[5];


   public void create(ActionEvent event)
   {

        randomNum = rand.nextInt(5);
        
        nameText = nameField.getText();
        name[randomNum] = nameText;
        
        label = "User created sucessfully at position " + randomNum;
        
    }

My other concern with the code is the new array I create for a search because the index for each element will change because all null elements are removed to prevent a nullpointerexception.

public void search(ActionEvent event)
{
    int count = 0;
    
    
    nameText = nameField.getText();
    
    name = Arrays.stream(name)
                 .filter(s -> (s != null && s.length() > 0))
                 .toArray(String[]::new);
    
    
    boolean contains = Arrays.stream(name).anyMatch(nameText::equals);
    
    
    if(contains == true)
    {
        while(!name[count].equals(nameText))
        {
            count++;
        }
        
        label = "User " + nameText + " is registered and is " + age[count];
        nameField.setText("");
        displayLabel.setText(label);
    }
    else
    {
        label = "User " + nameText + " is not registered.";
        nameField.setText("");
        displayLabel.setText(label);
    }
    
    
}

Solution

  • Keep track of the unused indexes and use those:

    SecureRandom rand = new SecureRandom();
    String label;
    
    
    String [] name = new String[5];
    List<Integer> unusedIndexes = new ArrayList<>();
    {
        for (int i = 0 ; i < name.length; i++) {
            unusedIndexes.add(Integer.valueOf(i));
        }
    }
    
    
    public void create(ActionEvent event) {
    
        if (unusedIndexes.isEmpty()) {
            // handle situation where no more slots are available
            return ;
        }
    
        int randomNum = unusedIndexes.remove(rand.nextInt(unusedIndexes.size())).intValue();
        
        nameText = nameField.getText();
        name[randomNum] = nameText;
        
        label = "User created sucessfully at position " + randomNum;
        
     }
    

    For the search, just search for the matching index. There's no need to remove null items first (s.equals(null) just returns false):

    public void search(ActionEvent event) {
    
        nameText = nameField.getText();
        int index = -1;
        for (int i = 0 ; i < name.length && index < 0; i++) {
            if (nameText.equals(name[i])) {
                index = i;
            }
        }        
        
        if(index >= 0) {      
            label = "User " + nameText + " is registered and is " + age[index];
            nameField.setText("");
            displayLabel.setText(label);
        } else {
            label = "User " + nameText + " is not registered.";
            nameField.setText("");
            displayLabel.setText(label);
        }        
    }
    

    Note it may be easier to implement this with a List instead of an array, e.g.

    SecureRandom rand = new SecureRandom();
    String label;
    
    
    List<String> name = new ArrayList<>();
    
    public void create(ActionEvent event) {
    
        int randomNum = rand.nextInt(name.size()+1);
        
        nameText = nameField.getText();
        name.add(randomNum, nameText);
        
        label = "User created sucessfully at position " + randomNum;
        
     }
    
     public void search(ActionEvent event) {
    
         nameText = nameField.getText();
         int index = name.indexOf(nameText);
        
         if (index >= 0) {      
             label = "User " + nameText + " is registered and is " + age.get(index);
             nameField.setText("");
             displayLabel.setText(label);
         } else {
             label = "User " + nameText + " is not registered.";
             nameField.setText("");
             displayLabel.setText(label);
         }        
     }