Matt23

Text in frame

May 21st, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. // ***************
  2. // * The         *
  3. // * strangest   *
  4. // * text        *
  5. // * that        *
  6. // * is          *
  7. // * going       *
  8. // * to          *
  9. // * be          *
  10. // * shown       *
  11. // * in          *
  12. // * a           *
  13. // * rectangular *
  14. // * frame       *
  15. // ***************
  16.  
  17.  
  18. public class TextInFrame {
  19.    
  20.     private static int getLongestStringLength(String[] array) {
  21.         int maxLength = 0;
  22.         for (String s : array)
  23.             if (s.length() > maxLength)
  24.                 maxLength = s.length();
  25.  
  26.         return maxLength;
  27.     }
  28.  
  29.     private static String repeatString(int count, String theChar) {
  30.         return new String(new char[count]).replace("\0", theChar);
  31.     }
  32.  
  33.     public static void main(String[] args) {
  34.         String[] text = "The strangest text that is going to be shown in a rectangular frame".split(" ");
  35.         int width = getLongestStringLength(text) + 4;
  36.  
  37.         String border = repeatString(width, "*");
  38.         System.out.println(border);
  39.  
  40.         for (String item : text)
  41.             System.out.format("* %s%s*\n", item, repeatString(width - 3 - item.length(), " "));
  42.  
  43.         System.out.println(border);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment