Search code examples
javaarraystostring

how to add a second return character after you run a toString method


I am doing a 3 part lab that requires me to write a toString method as one of the requirments. The to String method returns something like this when called:

Row 0 Col 0 
30.5 grams of ice 
42.3 grams of sulfur 
0.6 grams of silver 
 
Row 3 Col 5 
21.0 grams of water 
 
Row 3 Col 7 
45.2 grams of water 
31.2 grams of elixir 

My problem is not getting the information from the toString method, my issue is that I don't know how to add another return charachter between one Row and Column and another. For example, between .6 grams of silver and Ro 3 Col 5.

public String toString() {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            Vial vial = rack[i][j];
            if (vial != null) {
                result.append("Row " + i + " Col " + j + "\n");
                result.append(vial.toString());
            }
        }
    }
    return result.toString();
}

this is my current method and when I run it against the test I get this failure:

Row 0 Col 0
45.2 grams of sulfur
10.1 grams of lead
30.0 grams of ice
4.6 grams of elixirRow 1 Col 5
30.0 grams of ice

when it should look like this:

Row 0 Col 0
45.2 grams of sulfur
10.1 grams of lead
30.0 grams of ice
4.6 grams of elixir

Row 1 Col 5
30.0 grams of ice

I believe that this could be solved by an if statment but I am not sure what the parameters would be, I also think that it would make the most sense for the if statment(assuming it is correct to have one) to go after this line in my code

result.append("Row " + i + " Col " + j + "\n");                  
result.append(vial.toString());

I have to put something here


Solution

  • So this answer relies on another method named consolidate which was apparently wrong, so the correct answer is as follows:

     public void consolidate () {
        List<Vial> vials = new ArrayList<>();
    
        for (int r = 0; r < this.rack.length; r++) {
            for (int c = 0; c < this.rack[r].length; c++) {
                Vial v = this.rack[r][c];
    
                if (v != null) {
                    vials.add(v);
                    this.rack[r][c] = null;
                }
            }
        }
    
        int r = 0;
        int c = 0;
    
        while (vials.size() > 0) {
            this.rack[r][c] = vials.remove(0);
            c++;
            r += c / this.rack[r].length;
            c %= this.rack[r].length;
        }
    }
    
    @Override
    public String toString () {
        String str = "";
    
        for (int r = 0; r < this.rack.length; r++) {
            for (int c = 0; c < this.rack[r].length; c++) {
                Vial v = this.rack[r][c];
    
                if (v != null) {
                    System.out.printf("Row %d Col %d\n", r, c);
                    str += String.format("Row %d Col %d\n", r, c);
                    str += v.toString();
                    str += "\n\n";
                }
            }
        }
    
        return str.trim();
    }