Guest User

Untitled

a guest
Feb 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.42 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class ZipUnzip {
  5.    
  6.     public static void main(String[] args) {
  7.         userInterface();
  8.     }
  9.  
  10.     public static void userInterface() {
  11.         Scanner console = new Scanner(System.in);
  12.         System.out.print("Would you like to compress or decompress a file? : ");
  13.         String pressAnswer = console.next();
  14.         boolean Answer = false;
  15.         if (pressAnswer.equals("compress")) {
  16.             Answer = true;
  17.         }
  18.         else if (pressAnswer.equals("decompress")) {
  19.             Answer = false;
  20.         }
  21.         else {
  22.             System.out.println("Invalid command");
  23.             System.exit(1);
  24.         }
  25.        
  26.         System.out.print("What will be the name of the input file?: ");
  27.         Scanner in = getInputScanner(console);
  28.        
  29.         System.out.print("What will be the name of the output file?: ");
  30.         PrintStream out = getOutputPrintStream(console);
  31.        
  32.         processFile( Answer, in , out);
  33.        
  34.     }
  35.  
  36.     public static Scanner getInputScanner(Scanner console){
  37.         Scanner in = null; //null signifies NO object reference
  38.         while (in == null) {
  39.             System.out.print("input file name? ");
  40.             String name = console.next();
  41.             try {
  42.                 in = new Scanner(new File(name));
  43.             }
  44.             catch (FileNotFoundException e) {
  45.                 System.out.println("File not found. "
  46.                 + "Please try again.");
  47.             }
  48.         }
  49.         return in;
  50.     }
  51.  
  52.     public static PrintStream getOutputPrintStream(Scanner console){
  53.         String outfilename = console.next();
  54.         File f = new File(outfilename);
  55.        
  56.         if (f.exists()) {
  57.             System.out.println("The file exists.... exiting");
  58.             System.exit(1);
  59.         }
  60.  
  61.         PrintStream out = null;
  62.        
  63.         try {
  64.             out = new PrintStream(f);
  65.         }
  66.         catch (FileNotFoundException e) {
  67.             System.out.println(e.getMessage());
  68.             System.exit(1);
  69.         }  
  70.         return out;
  71.     }
  72.  
  73.     public static void processFile (boolean compress, Scanner input, PrintStream output){
  74.         if(compress == true) {
  75.             while (input.hasNextLine()) {
  76.                 String line = input.nextLine();
  77.                 output.println(compressLine(line));
  78.             }
  79.         }
  80.         else {
  81.             while (input.hasNextLine()) {
  82.                 String line = input.nextLine();
  83.                 output.println(decompressLine(line));
  84.                
  85.             }
  86.         }
  87.     }
  88.  
  89.     public static String compressLine(String line){
  90.         Scanner scan = new Scanner(line);
  91.         String compressedLine = "";
  92.         String compressedWord = "";
  93.         while(scan.hasNext()) {
  94.             String word = scan.next();
  95.             compressedWord = compress(word);
  96.             compressedLine = compressedLine + " " + compressedWord;
  97.         }
  98.         return compressedLine; 
  99.     }
  100.  
  101.     public static String compress(String word) {
  102.         String compressedWord = word;
  103.         if (word.length() < 2) {
  104.             word = compressedWord;
  105.         }
  106.         else if (word.length() == 2) {
  107.             if (word == "an") {
  108.                 compressedWord = "%";
  109.             }
  110.             else if (word == "re") {
  111.                 compressedWord = "@";
  112.             }  
  113.             else {
  114.                 compressedWord = word;
  115.             }
  116.         }
  117.         else if (word.length() > 2) {
  118.             for (int i = 2; i < compressedWord.length(); i++) {
  119.                 String search = compressedWord.substring(i-2, i+1);
  120.                
  121.                 if (search.equals("the")) {
  122.                     System.out.println("FLAG the");
  123.                     compressedWord = compressedWord.substring(0, i-2) + "~" + compressedWord.substring(i+1, compressedWord.length());
  124.                     break;
  125.                 }
  126.                 else {
  127.                     continue;
  128.                 }
  129.             }
  130.             for (int i = 2; i < compressedWord.length(); i++) {
  131.                 String search = compressedWord.substring(i-2, i+1);
  132.                 if (search.equals("ion")) {
  133.                     compressedWord = compressedWord.substring(0, i-2) + "&" + compressedWord.substring(i+1, compressedWord.length());
  134.                     break;
  135.                 }
  136.                 else {
  137.                     continue;
  138.                 }
  139.             }
  140.             for (int i = 2; i < word.length(); i++) {
  141.                 String search = word.substring(i-2, i+1);
  142.                 if (search.equals("ing")) {
  143.                     compressedWord = compressedWord.substring(0, i-2) + "#" + compressedWord.substring(i+1, compressedWord.length());
  144.                     break;
  145.                 }
  146.                 else {
  147.                     continue;
  148.                 }          
  149.             }
  150.             for (int i = 1; i < compressedWord.length(); i++) {
  151.                 String search = compressedWord.substring(i-1, i+1);
  152.                 if (search.equals("an")) {
  153.                     compressedWord = compressedWord.substring(0, i-1) + "%" + compressedWord.substring(i+1, compressedWord.length());
  154.                     break;
  155.                 }
  156.                 else {
  157.                     continue;
  158.                 }
  159.             }
  160.             for (int i = 1; i < compressedWord.length(); i++) {
  161.                 String search = compressedWord.substring(i-1, i+1);
  162.                 if (search.equals("re")) {
  163.                     compressedWord = compressedWord.substring(0, i-1) + "@" + compressedWord.substring(i+1, compressedWord.length());
  164.                     break;
  165.                 }
  166.                 else {
  167.                     continue;
  168.                 }
  169.             }
  170.             for (int i = 2; i < compressedWord.length(); i++) {
  171.                 String search = compressedWord.substring(i-2, i+1);
  172.                 if (search.equals("con")) {
  173.                     compressedWord = compressedWord.substring(0, i-2) + "$" + compressedWord.substring(i+1, word.length());
  174.                     break;
  175.                 }
  176.                 else {
  177.                     continue;
  178.                 }
  179.             }
  180.         }
  181.        
  182.         return compressedWord;
  183.     }
  184.  
  185.     public static String decompressLine(String line){
  186.         Scanner scan = new Scanner(line);
  187.         String decompressedLine = "";
  188.         String decompressedWord = "";
  189.         while(scan.hasNext()) {
  190.             String word = scan.next();
  191.             decompressedWord = decompress(word);
  192.             decompressedLine = decompressedLine + " " + decompressedWord;
  193.         }
  194.         return decompressedLine;
  195.     }
  196.  
  197.     public static String decompress(String word) {
  198.         String decompressedWord = word;
  199.         System.out.println(decompressedWord);
  200.        
  201.         if (decompressedWord.length() == 1) {
  202.             if (decompressedWord.equals("~")) {
  203.                 decompressedWord = "the";
  204.             }
  205.             else if (decompressedWord.equals("&")) {
  206.                 decompressedWord = "ion";
  207.             }
  208.             else if (decompressedWord.equals("#")) {
  209.                 decompressedWord = "ing";
  210.             }
  211.             else if (decompressedWord.equals("%")) {
  212.                 decompressedWord = "an";
  213.             }
  214.             else if (decompressedWord.equals("@")) {
  215.                 decompressedWord = "re";
  216.             }
  217.             else if (decompressedWord.equals("$")) {
  218.                 decompressedWord = "con";              
  219.             }
  220.         }  
  221.         else if (decompressedWord.length() > 1) {
  222.             for (int i = 0; i < decompressedWord.length() ; i++) {
  223.                 char search = decompressedWord.charAt(i);
  224.                 if (search == '~') {
  225.                     decompressedWord  = decompressedWord.substring(0, i) +"the" + decompressedWord.substring(i+1, decompressedWord.length());
  226.                 }
  227.             }
  228.             for (int i = 0; i < decompressedWord.length(); i++) {
  229.                 char search = decompressedWord.charAt(i);
  230.                 if (search == '&') {
  231.                     decompressedWord  = decompressedWord.substring(0, i) +"ion" + decompressedWord.substring(i+1, decompressedWord.length());
  232.                 }
  233.             }
  234.             for (int i = 0; i < decompressedWord.length(); i++) {
  235.                 char search = decompressedWord.charAt(i);
  236.                 if (search == '#') {
  237.                     decompressedWord  = decompressedWord.substring(0, i) +"ing" + decompressedWord.substring(i+1, decompressedWord.length());
  238.                 }
  239.             }
  240.             for (int i = 0; i < decompressedWord.length(); i++) {
  241.                 char search = decompressedWord.charAt(i);
  242.                 if (search == '%') {
  243.                     decompressedWord  = decompressedWord.substring(0, i) +"an" + decompressedWord.substring(i+1, decompressedWord.length());
  244.                 }
  245.             }
  246.             for (int i = 0; i < decompressedWord.length(); i++) {
  247.                 char search = decompressedWord.charAt(i);
  248.                 if (search == '@') {
  249.                     decompressedWord  = decompressedWord.substring(0, i) +"re" + decompressedWord.substring(i+1, decompressedWord.length());
  250.                 }
  251.             }
  252.             for (int i = 0; i < decompressedWord.length(); i++) {
  253.                 char search = decompressedWord.charAt(i);
  254.                 if (search == '$') {
  255.                     decompressedWord  = decompressedWord.substring(0, i) +"con" + decompressedWord.substring(i+1, decompressedWord.length());
  256.                 }
  257.             }
  258.         }
  259.         return decompressedWord;
  260.     }
  261. }
Add Comment
Please, Sign In to add comment