Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
    
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading