collegesista collegesista - 2 months ago 7
Java Question

Getting input from String ArrayList and output new strings

This code should take a String ArrayList and print out a modified version of these strings called "states". For example: The first string "3,3,L" should print MMMCCCL. Currently, when I input the ArrayList goodMoves: ([3,3,L, 1,1,R, 3,2,L, 0,3,R, 3,1,L, 2,2,R, 2,2,L, 3,1,R, 0,3,L, 3,2,R, 1,1,L, 3,3,R]), the code returns the last state in the ArrayList ("MMMCCCR") and not all 11 states:

MMMCCCL

MCR

MMMCCL

CCCR

MMMCL

MMCCR

MMCCL

MMMCR

CCCL

MMMCCR

MCL

MMMCCCR

This is what I have so far:

// Testing method
public static void main(String[] args) {

int missionaries = 3;
int cannibals = 3;
boolean BoatState = true;

// Add initial state
goodMoves.add(toString(missionaries, cannibals, BoatState));

// Call checkBoat and explore
explore(missionaries, cannibals, BoatState, goodMoves, allMovesMade);

// Print all steps using helper method
System.out.println(makePicture(goodMoves));

// Print the total number of operations made
String finalMoves = ("Number of moves made: " + (goodMoves.size()-1));
System.out.println(finalMoves);
}

// Create strings of state spaces
public static String makePicture(ArrayList<String> goodMoves) {

// Initialize strings
String missString = "";
String cannString = "";
String moveString = "";

// Use input to create printout
for (String output: goodMoves) {

// Get numbers/character at each index of string
char missNum = output.charAt(0);
char cannNum = output.charAt(2);
char boatPos = output.charAt(4);

// Print number of M's and C's then concatenate them together
missString = new String(new char[missNum]).replace("\0", "M");
cannString = new String(new char[cannNum]).replace("\0", "C");

// Concatenate both strings
moveString = missString + cannString + boatPos + "\n";
}
}
// Return string of current state
return moveString;
}


Please let me know if there are any clarifications needed. This problem is in reference to the common Missionaries and Cannibals game. Thanks in advance!

Update: problem solved. Thanks for the feedback

Answer

First of all, you have an if structure that is unnecessary. You have if (boatPos == 'L') and then you do something, then have else and then do the exact same thing. So just remove the if structure and do the thing.

So change your for loop to this:

// Use input to create printout
for (String output: goodMoves) {

    // Get numbers/character at each index of string
    char missNum = output.charAt(0);
    char cannNum = output.charAt(2);
    char boatPos = output.charAt(4);

    // Print number of M's and C's then concatenate them together
    missString = new String(new char[missNum]).replace("\0", "M");
    cannString = new String(new char[cannNum]).replace("\0", "C");

    // Concatenate both strings
    moveString = missString + cannString + boatPos + "\n";
}

Notice that on the last line, I added a "\n" to it. That is the newline character. It will print a new line after each moveString is calculated. If you need the line break to be somewhere else (I don't understand your code fully), then move it elsewhere. But adding a line break is as simple as that.