-1

How can I remove the item in an array that is selected by the random?.
Instead of giving me two different items in an array it's just printing the same item selected by the random.

String[] names = {"jey","abby","alyssa","cole","yzabel","miho"};
  
  Random rand = new Random();
   String names_rand = names[rand.nextInt(names.length)];

 
 for(int i = 0; i < 2; i++){
     System.out.println(names_rand);
 } 
Omar Shawky
  • 1,242
  • 1
  • 12
  • 25
koharoo
  • 3
  • 1
  • 1
    Yes, you are printing `System.out.println(names_rand);` twice. The value of `names_rand ` does not change in the loop. – Scary Wombat Sep 10 '21 at 00:35
  • 1
    The code does not seem to be related to your question *How can I remove the item in an array that is selected by the random?* – Scary Wombat Sep 10 '21 at 00:48
  • https://stackoverflow.com/questions/5533191/java-random-always-returns-the-same-number-when-i-set-the-seed – orubt Sep 10 '21 at 00:58
  • additionally visit https://stackoverflow.com/questions/5533191/java-random-always-returns-the-same-number-when-i-set-the-seed – orubt Sep 10 '21 at 01:00

2 Answers2

1

It's simply because any code outside the for loop won't run again, try this to run the random code every loop to get a new (possible same since it's random) string from the array

String[] names = {"jey","abby","alyssa","cole","yzabel","miho"};
Random rand = new Random();
 for(int i = 0; i < 2; i++){
   String names_rand = names[rand.nextInt(names.length)];
     System.out.println(names_rand);
}

as for deleting the item that would be somehow complicated using a string array as once it's allocated you can't add or delete from it (you can't modify it's size )unless you are willing to make a new temporary array, copy all of its items without the chosen string like this maybe :

    String[] names = {"jey", "abby", "alyssa", "cole", "yzabel", "miho"};
    Random rand = new Random();
    for (int i = 0; i < Math.min(2, names.length); i++) {
        int randInt = rand.nextInt(names.length), cpyIdx = 0;
        String[] namesTemp = new String[names.length - 1];
        for (int j = 0; j < names.length; j++) {
            if (j != randInt) {
                namesTemp[cpyIdx] = names[j];
                cpyIdx++;
            }
        }
        names = namesTemp;

a better version of this complicated code would be using an ArrayList, which allows to add and remove its items (change its size at runtime) easily with just one method call like this :

ArrayList<String> names = new ArrayList<>(Arrays.asList("jey", "abby", "alyssa", "cole", "yzabel", "miho"));
Random rand = new Random();
for (int i = 0; i < Math.min(2, names.size()); i++) {
     int randInt = rand.nextInt(names.size());
     names.remove(randInt);
}

you can read more about ArrayLists how to add/remove items through this link as well as many tutorials out there just write ArrayList java in google search engine

N.B : I've added Math.min(2, names.length) to the for loop condition as I was afraid you would get to the case that the array length would be less than the number of items you want to delete, using Math.min I'm ensuring that the for loop won't try access an item that isn't there in the array

Omar Shawky
  • 1,242
  • 1
  • 12
  • 25
1

If you are required to use Arrays then this way is probably among the best with the limitations involved.

  • generate a random number and get the name
  • create a new temporary array to hold the all names less the one removed.
  • copy all names up to but excluding the one removed to the temp array
  • copy all names just after the one excluded to the temp array.
  • assign the temp array to the original array
  • normal exceptions will be thrown if a null or empty array is used.

Random r = new Random();

String[] names =
        { "jey", "abby", "alyssa", "cole", "yzabel", "miho" };
ArrayList<Integer> a;

int idx = r.nextInt(names.length);
String name = names[idx];
String[] temp = new String[names.length - 1];
for (int i = 0; i < idx; i++) {
    temp[i] = names[i];
}
for (int i = idx; i < temp.length; i++) {
    temp[i] = names[i + 1];
}
names = temp;
System.out.printf("'%s' was removed.%n",name);
System.out.println(Arrays.toString(names));

Prints something like

'jey' has been removed.
[abby, alyssa, cole, yzabel, miho]

It's interesting to note that the best way would be to use an ArrayList as already given in a previous answer. But the primary reason is that an ArrayList does not need to make a temporary array but simply copies in place. Then it adjusts the size value, effectively hiding the garbage still in the internal array.

WJS
  • 36,363
  • 4
  • 24
  • 39