Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. //for loop
  2. System.out.printf("%d. %s tt $%.2fn",
  3. i + 1, BOOK_TYPE[i], COST[i]);
  4.  
  5. 1. Newspaper $1.00
  6. 2. Paper Back $7.50
  7. 3. Hardcover book $10.00
  8. 4. Electronic book $2.00
  9. 5. Magazine $3.00
  10.  
  11. System.out.printf("%2d. %-20s $%.2f%n", i + 1, BOOK_TYPE[i], COST[i]);
  12.  
  13. System.out.printf( "%10d. %25s $%25.2fn",
  14. i + 1, BOOK_TYPE[i], COST[i] );
  15.  
  16. String indent = " "; // 20 spaces.
  17.  
  18. String output = "Newspaper";
  19. output += indent.substring(0, indent.length - output.length);
  20.  
  21. public class Test {
  22. public static void main(String[] args) {
  23. String[] bookTypes = { "Newspaper", "Paper Back", "Hardcover book", "Electronic book", "Magazine" };
  24. double[] costs = { 1.0, 7.5, 10.0, 2.0, 3.0 };
  25.  
  26. // Find length of longest bookTypes value.
  27. int maxLengthItem = 0;
  28. boolean firstValue = true;
  29. for (String bookType : bookTypes) {
  30. maxLengthItem = (firstValue) ? bookType.length() : Math.max(maxLengthItem, bookType.length());
  31. firstValue = false;
  32. }
  33.  
  34. // Display rows of data
  35. for (int i = 0; i < bookTypes.length; i++) {
  36. // Use %6.2 instead of %.2 so that decimals line up, assuming max
  37. // book cost of $999.99. Change 6 to a different number if max cost
  38. // is different
  39. String format = "%d. %-" + Integer.toString(maxLengthItem) + "s tt $%9.2fn";
  40. System.out.printf(format, i + 1, bookTypes[i], costs[i]);
  41. }
  42. }
  43. }
  44.  
  45. public class ColourConsoleDemo {
  46. /**
  47. *
  48. * @param args
  49. *
  50. * "33[0m BLACK" will colour the whole line
  51. *
  52. * "33[37m WHITE33[0m" will colour only WHITE.
  53. * For colour while Opening --> "33[37m" and closing --> "33[0m"
  54. *
  55. *
  56. */
  57. public static void main(String[] args) {
  58. // TODO code application logic here
  59. System.out.println("33[0m BLACK");
  60. System.out.println("33[31m RED");
  61. System.out.println("33[32m GREEN");
  62. System.out.println("33[33m YELLOW");
  63. System.out.println("33[34m BLUE");
  64. System.out.println("33[35m MAGENTA");
  65. System.out.println("33[36m CYAN");
  66. System.out.println("33[37m WHITE33[0m");
  67.  
  68. //printing the results
  69. String leftAlignFormat = "| %-20s | %-7d | %-7d | %-7d |%n";
  70.  
  71. System.out.format("|---------Test Cases with Steps Summary -------------|%n");
  72. System.out.format("+----------------------+---------+---------+---------+%n");
  73. System.out.format("| Test Cases |Passed |Failed |Skipped |%n");
  74. System.out.format("+----------------------+---------+---------+---------+%n");
  75.  
  76. String formattedMessage = "TEST_01".trim();
  77.  
  78. leftAlignFormat = "| %-20s | %-7d | %-7d | %-7d |%n";
  79. System.out.print("33[31m"); // Open print red
  80. System.out.printf(leftAlignFormat, formattedMessage, 2, 1, 0);
  81. System.out.print("33[0m"); // Close print red
  82. System.out.format("+----------------------+---------+---------+---------+%n");
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement