Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:20:41+00:00 2026-06-12T02:20:41+00:00

In my ArrayList, i have get a random Item from my ArrayList for the

  • 0

In my ArrayList, i have get a random Item from my ArrayList for the equip method.

Whenever i use my tester, the terminal window prints java.lang.IllegalArgumentException: n must be positive when I try to call random for the size.

I tried to change the value of totalElements to the integer size of the elements, then the error would be an OutOfBoundsExeption

import java.util.*;
import java.util.Scanner;
import java.util.Random;
public class GameMaster {



    private int turn, totalElements;
    private boolean winner;
    private Avatar x1;
    private Avatar x2;
    private ArrayList<Item> inventory;

    public GameMaster(Avatar a1, Avatar a2)
    {

    x2 = a1;
    x1 = a2;
    turn = 1;
    winner = false;
    inventory = new ArrayList<Item>();
    totalElements = 0;

    }

    private void fillInventory()
    {

    inventory.add( new Item( "Zealot Blades", true, 8 ) );
    inventory.add( new Item( "BFG", true, 13 ) );
    inventory.add( new Item( "Synthetic Cloth", false, 7 ) );
    // more items 


    inventory.add( new Item( "Gauss Rifle", true, 9 ) );
    inventory.add( new Item( "Flight Unit", false, 6 ) );

    totalElements = inventory.size();


    }

    public String equip()
    {
    Avatar w;
    String a;
    if (turn%2==1)
            w=x2;
    else 
            w=x1;

    if (w.beltIsFull())
    {

            a = w.getName() + "'s belt is full. \n";
    }

    else
    {
            turn++;
            Random generator = new Random();  
            Item rand = inventory.get(generator.nextInt(totalElements));
            //terminal window in blueJ: java.lang.IllegalArgumentException: n must be positive              

            a = w.getName()+" is equiped with "+rand.getName()+".";



    }
    return a;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:20:42+00:00Added an answer on June 12, 2026 at 2:20 am

    The exception is telling you that totalElements is less than or equal to zero. And I think that’s because you haven’t initialized it. (I can see the fillInventory method, but I don’t see where your code is calling it …)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList of Routine objects and I must use this method to
I have a simple question. I try to get random values from a container
Get 'n' random string from a Arraylist of string. Where n= number of string
So i have this ArrayList: list.get(0) == love list.get(1) == foo list.get(2) == make
I have 4 arraylist in jsp and I want to get elements of lists
I have an activity which shows 3 random images from JSON data off the
What I'm trying to do here is to always get a random value from
I am doing some exercises from the book Thinking In Java . I have
I have an arraylist populated by four elements, the order of which is random
I'm learning Java and I'm having a problem with ArrayList and Random . I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.