Original Post
I just finished writing my first programming project of the quarter for my ICS class. My professor had us format the output using a String class method in java known as .format(). The TA helped me and my partner out but we're still lost about how exactly this works. Can someone explain this too me, please?
This is the example he used:
------------------------------------------------------------------------------
The first parameter you pass to it is called a format string, which is used to tell it how you'd like the data to be formatted, with placeholders for the data. Subsequent parameters specify the data that will replace the placeholders. Here's an example from a hypothetical Person class:
String s = "";
s += String.format("%-10s: %s\n", "Name", name);
s += String.format("%-10s: %s\n", "Title", title);
s += String.format("%-10s: %4d\n", "Height", height);
s += String.format("%-10s: %4d\n", "Weight", weight);
System.out.println(s);
-------------------------------------------------------------------------------
Thank you!
This is the example he used:
------------------------------------------------------------------------------
The first parameter you pass to it is called a format string, which is used to tell it how you'd like the data to be formatted, with placeholders for the data. Subsequent parameters specify the data that will replace the placeholders. Here's an example from a hypothetical Person class:
String s = "";
s += String.format("%-10s: %s\n", "Name", name);
s += String.format("%-10s: %s\n", "Title", title);
s += String.format("%-10s: %4d\n", "Height", height);
s += String.format("%-10s: %4d\n", "Weight", weight);
System.out.println(s);
-------------------------------------------------------------------------------
Thank you!