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

  • Home
  • SEARCH
  • 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 155251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:07:21+00:00 2026-05-11T10:07:21+00:00

Here is what I’m suppose to accomplish: Write a program that stimulates a bean

  • 0

Here is what I’m suppose to accomplish:

Write a program that stimulates a bean machine Your program should prompt the user to enter the number of balls and the number of slots in the machine. Simulate the falling of each ball by printing its path.

EX.

Enter the number of balls: 5
Enter the number of slots: 7

LRLRLRL
RRLRLLL
LLRRLLR
LRLLRLR
RRRLRRL
_ _ 0
_ _ 0
0 0 0

Here is my code so far:

import javax.swing.JOptionPane;         public static void main(String[] args) {             int balls=0;             int slots=0;             char [] direction= new char [slots];             int slot=0;             int i=0;             int path=0;              balls= Integer.parseInt(JOptionPane.showInputDialog('Enter' +                     ' the number of balls to be dropped:'));             slots= Integer.parseInt (JOptionPane.showInputDialog('Enter ' +                     'the number of slots:'));              for (int j=1;j<=balls;j++){                 while(i<slots){                     path= (int)(Math.random()*100);                     if (path <50){                         direction [slots]='L';                     }                     else{                         direction [slots]='R';                     }                 i++;                 slot++;             }             System.out.println('The pathway is' +direction[0]+direction[1]+direction[2]+direction[3]+direction[4]);         }     } 

There are a few things that I’m having problems with:

  1. In the last line of my code where I try to print the pathway I have to basically guess the number of slots the user selected. Is there a better way to print this?
  2. How can I print the number ‘balls’ that the user entered in the pattern as shown above?
  3. Are there any other problems with my code?
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T10:07:22+00:00Added an answer on May 11, 2026 at 10:07 am

    Well, for starters, I’m getting a consistent ArrayIndexOutOfBoundsException on the line direction[slots] = 'L'; (or 'R'). That’s because direction is always of length 0, since you initialized it to slots when slots was 0. Move the line

    char [] direction= new char [slots]; 

    to after slots is input.

    Next, you always assign the ‘L’ or ‘R’ to the position immediately after the end of the array. That’s another reason for the ArrayIndexOutOfBoundsException I was getting. Change the assignment to

    direction[i] = 'L'; // or 'R' 

    Next, you don’t reset i after the while loop. So the path is calculated only for the first ball and then reused for all the others. I would make it a for loop instead, like this:

    for (i = 0; i < slots; i++) {     // your code here (make sure you don't change i inside the loop) } 

    Finally, as others have said, you should be using a loop to print out the path. You know how long the direction array is (it’s direction.length, if you didn’t know), so you can just loop through it and print out each letter.

    Once you’ve made these changes, your program should work (edit: except that it doesn’t keep track of which slot each ball ends up in). It will still have some room for improvement, but finding those things is part of the fun–isn’t it?

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

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer According to W3: Partial URLs are interpreted relative to the… May 11, 2026 at 9:19 pm
  • Editorial Team
    Editorial Team added an answer Googling doesn't seem to help much, but it seems to… May 11, 2026 at 9:19 pm
  • Editorial Team
    Editorial Team added an answer Write the properties methods by hand: public class Deal {… May 11, 2026 at 9:19 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Here is what I have: JAVA_HOME=C:\Software\Java\jdk1.5.0_12 (points to JDK 5.0) In Eclipse Installed Runtimes
Here is what I am trying to do: Given a date, a day of
Here is what I need to be able to do: I need to match

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.