0

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)

10
  • 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) Commented Feb 10, 2018 at 18:17
  • 1
    Also, show your main method and code as a minimal reproducible example Commented Feb 10, 2018 at 18:17
  • 2
    You never called CreateArrayList().... Commented Feb 10, 2018 at 18:17
  • 2
    int z = rand.nextInt(10); should be int z = rand.nextInt(myArray.size()); Commented Feb 10, 2018 at 18:19
  • 1
    Call CreateArrayList() before calling showOperations() method. Commented Feb 10, 2018 at 18:21

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Well, that would be useful, if the size of that list would change. Since there are exactly 10 elements, he already passes the size of the list. Telling him, he needs to pass the size of the list is wrong and in contradiction to the advice, to not pass 10, which is the size of the list. It's more robust against experiments - I don't dispute that. :)
The size of the list is only 10 if the add methods were actually ran, so therefore the size of the list does change
No. myArray.size () doesn't heal the problem. If the problem is healed (calling the array initializing code), the part you we don't like needs not to be fixed. Fixing it would be nice, but is for such fire-and-forget-code overengineering. It's even a theorem prover, if the code is run often enough (The theorem here is: The list contains 10 elements).
0

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 );
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.