Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //for loop
- System.out.printf("%d. %s tt $%.2fn",
- i + 1, BOOK_TYPE[i], COST[i]);
- 1. Newspaper $1.00
- 2. Paper Back $7.50
- 3. Hardcover book $10.00
- 4. Electronic book $2.00
- 5. Magazine $3.00
- System.out.printf("%2d. %-20s $%.2f%n", i + 1, BOOK_TYPE[i], COST[i]);
- System.out.printf( "%10d. %25s $%25.2fn",
- i + 1, BOOK_TYPE[i], COST[i] );
- String indent = " "; // 20 spaces.
- String output = "Newspaper";
- output += indent.substring(0, indent.length - output.length);
- public class Test {
- public static void main(String[] args) {
- String[] bookTypes = { "Newspaper", "Paper Back", "Hardcover book", "Electronic book", "Magazine" };
- double[] costs = { 1.0, 7.5, 10.0, 2.0, 3.0 };
- // Find length of longest bookTypes value.
- int maxLengthItem = 0;
- boolean firstValue = true;
- for (String bookType : bookTypes) {
- maxLengthItem = (firstValue) ? bookType.length() : Math.max(maxLengthItem, bookType.length());
- firstValue = false;
- }
- // Display rows of data
- for (int i = 0; i < bookTypes.length; i++) {
- // Use %6.2 instead of %.2 so that decimals line up, assuming max
- // book cost of $999.99. Change 6 to a different number if max cost
- // is different
- String format = "%d. %-" + Integer.toString(maxLengthItem) + "s tt $%9.2fn";
- System.out.printf(format, i + 1, bookTypes[i], costs[i]);
- }
- }
- }
- public class ColourConsoleDemo {
- /**
- *
- * @param args
- *
- * "