Advertisement
Guest User

Squeeze

a guest
Dec 11th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.PrintWriter;
  3. import java.util.Scanner;
  4.  
  5. /*
  6. * method to get rid of spaces of shitty words
  7. */
  8. public class Squeeze
  9. {
  10.  
  11. public static void squeezeAll()
  12. {
  13. try
  14. {
  15. Scanner scanner = new Scanner(new File("src\\input.txt"));
  16. PrintWriter pw = new PrintWriter(new File("src\\output.txt"));
  17.  
  18. while (scanner.hasNextLine())
  19. {
  20. String line = scanner.nextLine();
  21. String newLine = "";
  22.  
  23. for (int i = 0; i < line.length(); i++)
  24. {
  25. if (!Character.isWhitespace(line.charAt(i)))
  26. newLine += line.charAt(i);
  27. }
  28.  
  29. pw.println(newLine);
  30. }
  31.  
  32. pw.flush();
  33. pw.close();
  34. scanner.close();
  35. }
  36. catch(Exception e)
  37. {
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. public static void squeezeFile()
  43. {
  44. try
  45. {
  46. Scanner scanner = new Scanner(new File("src\\input.txt"));
  47. PrintWriter pw = new PrintWriter(new File("src\\output.txt"));
  48.  
  49. while (scanner.hasNextLine())
  50. {
  51. pw.println(trim(scanner.nextLine()));
  52. }
  53.  
  54. pw.flush();
  55. pw.close();
  56. scanner.close();
  57. }
  58. catch(Exception e)
  59. {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public static String trim(String str)
  65. {
  66. int firstSpace = 0;
  67. int lastSpace = str.length() - 1;
  68.  
  69. for(int i = 0; i < str.length(); i++)
  70. {
  71. if (!isWhitespace(str.charAt(i)))
  72. firstSpace = i;
  73. }
  74.  
  75. for (int i = str.length() - 1; i >= 0; i++)
  76. {
  77. if (!isWhitespace(str.charAt(i)))
  78. lastSpace = i;
  79. }
  80.  
  81. return str.substring(firstSpace, lastSpace);
  82. }
  83.  
  84. public static boolean isSpace(char c)
  85. {
  86. return c == ' ';
  87. }
  88.  
  89. public static boolean isTab(char c)
  90. {
  91. return c == ' ';
  92. }
  93.  
  94. public static boolean isWhitespace(char c)
  95. {
  96. return c == ' ' || c == ' ';
  97. }
  98.  
  99. public static String squeeze(String word)
  100. {
  101. String newWord = "";
  102.  
  103. for (int i = 0; i < word.length(); i++)
  104. {
  105. if (!Character.isWhitespace(word.charAt(i)))
  106. newWord += word.charAt(i);
  107. }
  108.  
  109. return newWord;
  110. }
  111.  
  112. public static void main(String[] args)
  113. {
  114. System.out.println(Squeeze.squeeze(" W O R D "));
  115. Squeeze.squeezeAll();
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement