java.io.PrintStream display

I’m working on a java program using arrays and loops to create a table, however when the values print they are followed by “[email protected]” repeating over and over a number of times

The chunk of code causing the error is as follows, more specifically the “row +=” sections. Any help for how to get rid of the repeated part at the end would be appreciated.

for ( int i = starting; i <= ending; i+= 1){
    row += System.out.format("%6d" + ": ", i);

    for ( int j = 0; j <= 11; j+=1){
        double answer = i*octaveArray[j];
        row += System.out.format("%.0f ", answer );
    }
    System.out.printf(row);
    System.out.println("");
}

Answer

From the documentation of PrintStream#format():

Writes a formatted string to this output stream using the specified format string and arguments.

That means that PrintStream#format() will write the values to the output stream but you then append its toString representation which looks like [email protected] to the row variable which you then print out to the same output stream.

You should use String.format() instead if you wish to append the formatted result to a String variable.

Leave a Reply

Your email address will not be published. Required fields are marked *