Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I receiving "bound must be positive" error?

Tags:

java

random

I am trying to write code that simulates a game of Scrabble. I designed a class which should simulate a Scrabble bag, and I am trying to test it by printing the tileID in main() after choosing a random tile. Whenever I run the code though, I keep getting the following error:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Random.java:388)
    at hw3.RandomBag.randomPick(RandomBag.java:39)
    at hw3.RandomBag.main(RandomBag.java:59`

Can somebody tell me why I am receiving that error?

import java.util.*;

public class RandomBag<E> implements Iterable<E> {
    
    // instance varibles
    private List<E> bag; // arraylist as the container
    private Random rand; // random number generator
    
    // constructors
    public RandomBag() { 
        bag = new ArrayList<E>();
        rand = new Random();    
    }
    
    public RandomBag(int seed) { 
        bag = new ArrayList<E>();
        rand = new Random(seed);
        
    }
    
    // returns the size of the bag
    public int size() { return this.bag.size(); }  
    
    public boolean isEmpty() { // returns true/false if the bag is/is not empty
        if (this.bag.isEmpty())
            return true;
        else
            return false;
    }  
    
    // adds the parameter element in the bag
    public void add (E element) {this.bag.add(element);} 
    
    // randomly selects an element using the random number generator 'rand' and removes that element from the bag, and returns the element
    public E randomPick() {
        
        int index = rand.nextInt(this.bag.size());
        E tileID = bag.remove(index);
        return tileID;
    } 
    
    // obtains an iterator for the bag, and returns it
    public Iterator<E> iterator() {
        // traverse bag using an iterator
        Iterator it = bag.iterator();
        return it;
    }
    
      //**
      //** main() for testing RandomBag<E>
      //**
    public static void main(String[] args) {
        
        RandomBag bag = new RandomBag();   
        
        Object tileID = bag.randomPick(); 
        System.out.println(tileID);

    }
}
like image 758
Omar N Avatar asked Dec 14 '22 11:12

Omar N


1 Answers

If this.bag.size() is 0, you are passing an invalid argument to nextInt().

This is clearly stated in the Javadoc :

n the bound on the random number to be returned. Must be positive.

nextInt(n) returns a number between 0 and n-1. What do you expect nextInt(0) to return?

In your main method you are attempting to pick an element of an empty bag. It can't work. You should check the size of the bag before calling randomPick(). And randomPick() should probably throw an exception when the bag is empty.

public static void main(String[] args) {

    RandomBag bag = new RandomBag();   

    Object tileID = null;
    if (bag.size() > 0)
        tileID = bag.randomPick(); 
    System.out.println(tileID);

}
like image 111
Eran Avatar answered Dec 28 '22 15:12

Eran