user7026854 user7026854 - 7 days ago 4x
Java Question

Java - I need to print this string of characters to a string I can use out of loop

I have been buried in this assignment for 2 days chasing down rabbit holes for possible solutions. I am beginner Java, so I am sure this shouldn't be as difficult as I am making it.

I trying to program the infamous Java Bean Machine... My professor want the Class Path to return a String Variable that only holds "R" "L" . to represent the path of the dropped ball.

Each ball should have its own Path... I can get the path... but I can not get the path to print in a string outside of the for/if statement.

Here are his instructions... in case you can see if I am interpreting this incorrectly.

Please help!! Thank you in advance for sifting through this....

my code so far ******** i have updated the code to reflect the suggestions.. Thank you... ***************** New problem is it repeats the series of letters in a line... I only need a string of 6 char ....(LRLLRL)

public class Path {
StringBuilder myPath;

public Path() {
myPath = new StringBuilder();


void moveRight() {



void moveLeft() {


public void fallLevels(int levels) {

levels = 6;

for (int i = 0; i < (levels); i++) {

if (Math.random() < 0.5) {

} else {



public String getPath() {


return myPath.toString();



******Thank you all.. this class now returns the correct string for one ball...***************

here is my code so far for multiple balls... I can get a long continuous string of 6 character sequences... I need each sequence to be a searchable string...I am not sure if I need to alter the Path class or if its something in the simulateGame() method. I think I can take it after this hump... Thank you again....

public class BeanMachine {
int numberOfLevels;
int[] ballsInBins;
Path thePath = new Path();

public BeanMachine(int numberOfLevels) {
this.numberOfLevels = 6;
ballsInBins = new int[this.numberOfLevels + 1];
// this.numberOfLevels +

public void simulateGame(int number) {

//looping through each ball
for (int i = 0; i < numberOfLevels -1; i++) {



*** this isn't the entire code for this class... I have to get this method correct to continue....


Problem with your code:

if (Math.random() < 0.5) {
                **loop = this.myPath = "R";**
            } else {
                 **loop = this.myPath ="L";**


Change this to:

if (Math.random() < 0.5) {
                **loop = this.myPath + "R";**
            } else {
                 **loop = this.myPath + "L";**


Just added ** to highlight where there is wrong in your code