简体   繁体   中英

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 "java.io.PrintStream@1909752" 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("");
}

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 java.io.PrintStream@1909752 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM