Advertisement
Guest User

Compressing a string

a guest
Mar 29th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Map;
  8. import java.util.Scanner;
  9. import java.util.Set;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. public class Menu {
  14. boolean exit;
  15.  
  16. public static void main(String[] args) {
  17. Menu menu = new Menu();
  18. menu.runMenu();
  19. }
  20.  
  21. public void runMenu() {
  22. printHeader();
  23. while(!exit) {
  24. printMenu();
  25. int optionsea = getInput();
  26. performAction(optionsea);
  27. }
  28. }
  29.  
  30. private void performAction(int optionsea) {
  31. switch(optionsea) {
  32. case 0:
  33. exit = true;
  34. System.out.println("File (Compressor / Cecompressor) Exited");
  35. break;
  36. case 1:
  37. compressFile();
  38. break;
  39. case 2:
  40. decompressFile();
  41. break;
  42. default:
  43. System.out.println("Unknown error, unlucks, no idea how you got here");
  44. }
  45. }
  46.  
  47. private int getInput() {
  48. Scanner seascanner = new Scanner(System.in);
  49. int optionsea = -1;
  50. while(optionsea < 0 || optionsea > 2) {
  51. try {
  52. System.out.print("Enter value: ");
  53. optionsea = Integer.parseInt(seascanner.nextLine());
  54. }
  55. catch(NumberFormatException e) {
  56. System.out.println("Invalid selection");
  57. }
  58. }
  59. return optionsea;
  60. }
  61.  
  62. private void printHeader() {
  63. System.out.println("File (Compressor / Cecompressor) Launched");
  64. System.out.println("Uncompressed file -> Compressed file (uncompressed.txt)");
  65. System.out.println("Compressed file -> Uncompressed file (compressed.txt) \n");
  66. }
  67.  
  68. private void printMenu() {
  69. System.out.println("Please make a selection");
  70. System.out.println("0 - Exit menu");
  71. System.out.println("1 - Compress file");
  72. System.out.println("2 - Uncompress file \n");
  73.  
  74. }
  75.  
  76. private static final String seafile = "C:\\Users\\mpspo\\Documents\\NetBeansProjects\\newFile\\uncompressed.txt";
  77.  
  78. public void compressFile() {
  79.  
  80. BufferedReader seaReader = null;
  81. FileReader seaFileReader = null;
  82. String seaCurrentLine = null;
  83.  
  84. try {
  85. seaFileReader = new FileReader(seafile);
  86. seaReader = new BufferedReader(seaFileReader);
  87.  
  88. seaReader = new BufferedReader(new FileReader(seafile));
  89. while ((seaCurrentLine = seaReader.readLine()) != null) {
  90. System.out.println("\nFile read: uncompressed.txt");
  91. String sea1 = seaCurrentLine;
  92.  
  93. String sea2 = sea1.toLowerCase();
  94. String sea3 = sea2.replaceAll("[!.,:;?]", " "); //remove punctuation + sets to lower case
  95. String sea4 = sea3.replaceAll(" ", " "); //closes gaps
  96. String sea5 = sea4.replaceAll(" ", ","); //allows it to be converted into array
  97. String sea6 = sea5.replaceAll("[']", " "); //remove appostrophies (different to all other grammer
  98. String sea7 = sea6.replaceAll(" ", ""); //closes appostrophie gap and any other error gap
  99. System.out.println("\nOriginal: " + sea7); // prints the string and seperates from line below
  100.  
  101. String[] sealist = sea7.split(","); //splits string into arraylist (so it can be looped)
  102.  
  103. Map<String, Set<Integer>> seaMap = new HashMap<>(); //new hashmap
  104. for (int seaInt = 0; seaInt < sealist.length; seaInt++) {
  105. if (seaMap.keySet().contains(sealist[seaInt])) {
  106. Set<Integer> index = seaMap.get(sealist[seaInt]);
  107. index.add(seaInt);
  108. } else {
  109. Set<Integer> index = new HashSet<>();
  110. index.add(seaInt);
  111. seaMap.put(sealist[seaInt], index);
  112. }
  113. }
  114. System.out.print("Compressed: ");
  115. seaMap.forEach((seawords, seavalues) -> System.out.print(seawords + seavalues + ",")); //prints words and indexes of words to console
  116. System.out.println("\n");
  117.  
  118. try{
  119. PrintWriter writer = new PrintWriter("compressed.txt", "UTF-8"); //setup of printwriter & file name & text method
  120. seaMap.forEach((seawords, seavalues) -> writer.print(seawords + seavalues + ",")); // foreach loop, writer prints words and indexes
  121. writer.close(); //writer closed
  122. }
  123. catch (IOException e) { //catch failures in writer
  124. System.err.print("Writer Error"); //prints error message
  125. }
  126. }
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. } finally {
  130. if (seaReader != null)
  131. try {
  132. seaReader.close();
  133. } catch (IOException ex) {
  134. Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
  135. }
  136. if (seaFileReader != null)
  137. try {
  138. seaFileReader.close();
  139. } catch (IOException ex) {
  140. Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
  141. }
  142. }
  143. }
  144.  
  145. public void decompressFile() {
  146.  
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement