简体   繁体   中英

Is it possible to implement tostring string method and printf?

I'm a beginner to Java. My program is given an output in tabular form, and it returns from a method called **calculateShoes. I'm just wondering if it is possible to do printf while return is on there. I tried putting a System.out.printf (after the return statement) to use the "|%3d|" for spacing but it's not working.

@Override public String toString() { return "| " + this.numberOfShoes + " | " + this.sumOfShoesInputted + " | " + this.lowestNumberOfShoes + " to " + this.highestNumberOfShoes + "|"; }

I realize that this looks messy, and the data outputted moves the "|". I would like to have it fixed and not resized all the time. I hope there's a cleaner way to do this.

**Sorry I had the wrong boolean method >_<.

You can try using String.format() :

return String.format("|%3d|", some_variable);

This method works similarly to System.out.printf()

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