Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.PrintWriter;
- import java.util.Scanner;
- /*
- * method to get rid of spaces of shitty words
- */
- public class Squeeze
- {
- public static void squeezeAll()
- {
- try
- {
- Scanner scanner = new Scanner(new File("src\\input.txt"));
- PrintWriter pw = new PrintWriter(new File("src\\output.txt"));
- while (scanner.hasNextLine())
- {
- String line = scanner.nextLine();
- String newLine = "";
- for (int i = 0; i < line.length(); i++)
- {
- if (!Character.isWhitespace(line.charAt(i)))
- newLine += line.charAt(i);
- }
- pw.println(newLine);
- }
- pw.flush();
- pw.close();
- scanner.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- public static void squeezeFile()
- {
- try
- {
- Scanner scanner = new Scanner(new File("src\\input.txt"));
- PrintWriter pw = new PrintWriter(new File("src\\output.txt"));
- while (scanner.hasNextLine())
- {
- pw.println(trim(scanner.nextLine()));
- }
- pw.flush();
- pw.close();
- scanner.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- public static String trim(String str)
- {
- int firstSpace = 0;
- int lastSpace = str.length() - 1;
- for(int i = 0; i < str.length(); i++)
- {
- if (!isWhitespace(str.charAt(i)))
- firstSpace = i;
- }
- for (int i = str.length() - 1; i >= 0; i++)
- {
- if (!isWhitespace(str.charAt(i)))
- lastSpace = i;
- }
- return str.substring(firstSpace, lastSpace);
- }
- public static boolean isSpace(char c)
- {
- return c == ' ';
- }
- public static boolean isTab(char c)
- {
- return c == ' ';
- }
- public static boolean isWhitespace(char c)
- {
- return c == ' ' || c == ' ';
- }
- public static String squeeze(String word)
- {
- String newWord = "";
- for (int i = 0; i < word.length(); i++)
- {
- if (!Character.isWhitespace(word.charAt(i)))
- newWord += word.charAt(i);
- }
- return newWord;
- }
- public static void main(String[] args)
- {
- System.out.println(Squeeze.squeeze(" W O R D "));
- Squeeze.squeezeAll();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement