Adndfd
Adndfd

Reputation: 1

Getting a random item from a ArrayList<String>

I want to select a random string from my ArrayList and then print that string. here is my code:

 public class Operator {

Random rand = new Random();
ArrayList<String> myArray = new ArrayList<String>();
public void CreateArrayList() {
     myArray.add("add");
     myArray.add("subtract");
     myArray.add("multiply");
     myArray.add("divide");
     myArray.add("remainder");
     myArray.add("greaterthan");
     myArray.add("lessthan");
     myArray.add("max");
     myArray.add("min");
     myArray.add("power");
     try {
            FileReader inFile = new FileReader("data/numbers2.txt");
            Scanner scanner = new Scanner(inFile);
            String line = scanner.nextLine();
            System.out.println(line);

            scanner.close();
        } 
        catch (Exception ex) {
            ex.printStackTrace();}
    }
{

}

public void showOperations() {
    int x = (int) Math.floor(Math.random()*10);
    int y = (int) Math.floor(Math.random()*10);
    int z = rand.nextInt(10);
    System.out.println(x+" "+ myArray.get( z )+" "+ y );
    }
}

The output should be for example "3 add 4". However, ever time I run the code, I get

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at hw2p2.Operator.showOperations(Operator.java:42) at hw2p2.Launcher.main(Launcher.java:9)

Upvotes: 0

Views: 320

Answers (2)

user9190555
user9190555

Reputation:

You never actually created the arrayList, so when you call the array in your showoperations method, it ends up trying to do the operations on an empty array. In addition, I tweaked your calculation for z so that it will give you a number no matter the sizeof the arrayList.

    import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;


public class Operator {

Random rand = new Random();
ArrayList<String> myArray = new ArrayList<String>();

public void createArrayList() {
     myArray.add("add");
 myArray.add("subtract");
 myArray.add("multiply");
 myArray.add("divide");
 myArray.add("remainder");
 myArray.add("greaterthan");
 myArray.add("lessthan");
 myArray.add("max");
 myArray.add("min");
 myArray.add("power");
 try {
        FileReader inFile = new FileReader("data/numbers2.txt");
            Scanner scanner = new Scanner(inFile);
            String line = scanner.nextLine();
            System.out.println(line);

            scanner.close();
        } 
        catch (Exception ex) {
            ex.printStackTrace();}
    }

public void showOperations() {
    createArrayList();
    int x = (int) Math.floor(Math.random()*10);
    int y = (int) Math.floor(Math.random()*10);
    int z = rand.nextInt(myArray.size());
    System.out.println(x+" "+ myArray.get( z )+" "+ y );
    }
}

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191973

rand.nextInt(10); gets a random number with no respect to the content of your list.

You need to pass the size of the list there, not literally 10 using myArray.size()

Secondly, if you want your code to do anything other than choose a random element from an empty list, you'll need to call that other method

It's a list, not an array

Upvotes: 1

Related Questions