fork download
  1. class FramedWords
  2. {
  3. public final String TOP_BOTTOM = "_";
  4. public final String SIDE = "|";
  5. public final String SPACE = " ";
  6.  
  7. public static void main(String[] args)
  8. {
  9. new FramedWords().frame("Hello cruel world, this is not lovely at all.");
  10. }
  11.  
  12. public void frame(String input)
  13. {
  14. String[] inputs = input.split("[\\s:\\-,.]");
  15. int sideHeight = inputs.length - 1;
  16. int width = 0;
  17. for (String s : inputs)
  18. {
  19. if (s.length() > width)
  20. {
  21. width = s.length();
  22. }
  23. }
  24.  
  25. for (int i = 0; i < width + 2; ++i)
  26. {
  27. System.out.print(TOP_BOTTOM);
  28. }
  29.  
  30. System.out.println();
  31.  
  32. for (int i = 0; i < sideHeight; ++i)
  33. {
  34. System.out.print(SIDE);
  35. System.out.print(inputs[i]);
  36. for (int j = 0; j < width - inputs[i].length(); ++j)
  37. {
  38. System.out.print(SPACE);
  39. }
  40. System.out.println(SIDE);
  41. }
  42.  
  43. System.out.print(SIDE);
  44. System.out.print(inputs[sideHeight]);
  45. for (int i = 0; i < width - inputs[sideHeight].length(); ++i)
  46. {
  47. System.out.print(TOP_BOTTOM);
  48. }
  49. System.out.print(SIDE);
  50. }
  51. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
________
|Hello |
|cruel |
|world |
|      |
|this  |
|is    |
|not   |
|lovely|
|at    |
|all___|