Advertisement
MegaZeroX

Untitled

Apr 23rd, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.nio.file.Files;
  4. import javax.swing.JFileChooser;
  5. /**
  6. * Creates a frequency table of characters found in a file
  7. * @author Matthew Ferland
  8. */
  9. public class FTable{
  10. //List of characters and frequencies in this list.
  11. private ArrayList<PairNode<Character, Integer>> list;
  12. //File to be read from
  13. private File file;
  14. /**
  15. * Constructor for this class. Creates an empty ArrayList.
  16. * @param file File that the FTable is to be read from.
  17. */
  18. public FTable(File file){
  19. this.list = new ArrayList<PairNode<Character, Integer>>();
  20. this.file = file;
  21. formTable();
  22. outputToFile();
  23. System.out.println(this);
  24. }
  25. /**
  26. * Creates a FTable from file, and outputs frequency to new file.
  27. */
  28. public void formTable(){
  29. String string = fileToString();
  30. this.frequencyAnalysis(string);
  31. }
  32. /**
  33. * Turns a string into a series of pairs of characters and their frequency
  34. * @param string String to be converted.
  35. */
  36. private void frequencyAnalysis(String string){
  37. String charsTaken = "";
  38. for(int i = 0; i < string.length(); i++){
  39. if(charsTaken.contains(String.valueOf(string.charAt(i)))){
  40. continue;
  41. }
  42. else{
  43. char c = string.charAt(i);
  44. charsTaken += c;
  45. int count = 0;
  46. for(int j = i; j < string.length(); j++){
  47. if(string.charAt(j) == c){
  48. count++;
  49. }
  50. }
  51. PairNode<Character, Integer> pair = new PairNode<Character,Integer>(c, count);
  52. list.add(pair);
  53. }
  54. }
  55. }
  56. /**
  57. * Outputs this into a file of the user's choice.
  58. */
  59. private void outputToFile(){
  60. System.out.print("Would you like to send the frequency to a specific output file?");
  61. System.out.println(" If so, type 'y' or 'yes'. Else, type 'n' or 'no'.");
  62. String s = "";
  63. final Scanner sc = new Scanner(System.in);
  64. while (!s.equals("y") && !s.equals("yes")){
  65. s = sc.nextLine();
  66. if(s.equals("n") || s.equals("no")) return;
  67. }
  68. final JFileChooser fc = new JFileChooser();
  69. fc.setSelectedFile(new File("frequency.txt"));
  70. int returnVal = fc.showSaveDialog(null);
  71. File file = fc.getSelectedFile();
  72. try{
  73. FileWriter writer = new FileWriter(file);
  74. String out = this.toString();
  75. writer.append(out);
  76. writer.flush();
  77. writer.close();
  78. }
  79. catch(IOException e){
  80. System.out.println("Cancelled and aborted");
  81. }
  82. }
  83. /**
  84. * Takes the file passed into this by the instructor into a string.
  85. * @return A string version of the file to be read in.
  86. */
  87. private String fileToString(){
  88. List<String> strings = new ArrayList<String>();
  89. try{
  90. strings = Files.readAllLines(this.file.toPath());
  91. }
  92. catch(IOException e){}
  93. //String that holds string version of file.
  94. String string = "";
  95. //Takes the list of strings and turns it into a single string
  96. for(int i = 0; i < strings.size(); i++){
  97. if(i < strings.size() - 1){
  98. string += strings.get(i) + "\n";
  99. }
  100. else{
  101. string += strings.get(i);
  102. }
  103. }
  104. return string;
  105. }
  106. /**
  107. * Returns a string frequency table.
  108. * @return A string version of this.
  109. */
  110. public String toString(){
  111. String string = "";
  112. for(int i = 0; i < this.list.size(); i++){
  113. if(this.list.get(i).getFirst() == '\n'){
  114. string += "\\n: " + this.list.get(i).getSecond() + "\n";
  115. }
  116. else if(this.list.get(i).getFirst() == ' '){
  117. string += "(space): " + this.list.get(i).getSecond() + "\n";
  118. }
  119. else if(this.list.get(i).getFirst() == '\t'){
  120. string += "\\t: " + this.list.get(i).getSecond() + "\n";
  121. }
  122. else{
  123. string += this.list.get(i) + "\n";
  124. }
  125. }
  126. return string;
  127. }
  128. }
  129.  
  130. /**
  131. * Represents a Pair of values.
  132. * @author Matthew Ferland
  133. */
  134. public class PairNode<FirstType, SecondType> {
  135. //the first element in the pair
  136. private FirstType first;
  137. //the second element in the pair
  138. private SecondType second;
  139. //The left child of this node
  140. private PairNode<FirstType, SecondType> left;
  141. //The right child of this node
  142. private PairNode<FirstType, SecondType> right;
  143. /**
  144. * Class constructor
  145. * @param first The first element in this Pair
  146. * @param second The second element in this Pair
  147. */
  148. public PairNode(FirstType first, SecondType second) {
  149. this.first = first;
  150. this.second = second;
  151. this.left = null;
  152. this.right = null;
  153. }
  154. /**
  155. * Returns the first element.
  156. * @return The first element in this.
  157. */
  158. public FirstType getFirst() {
  159. return this.first;
  160. }
  161. /**
  162. * Returns the second element.
  163. * @return The second element in this.
  164. */
  165. public SecondType getSecond() {
  166. return this.second;
  167. }
  168. /**
  169. * Sets the first element.
  170. * @param newFirst The value to set the first element to.
  171. */
  172. public void setFirst(FirstType newFirst) {
  173. this.first = newFirst;
  174. }
  175. /**
  176. * Sets the first element.
  177. * @param newFirst The value to set the first element to.
  178. */
  179. public void setSecond(SecondType newSecond) {
  180. this.second = newSecond;
  181. }
  182. /**
  183. * Tests whether two Pairs are equal.
  184. * @param other Another pair that might be equivalent to this.
  185. * @return True if both the first element of this equals the first element of other
  186. and the second element of this equals the second element of other, false otherwise.
  187. */
  188. public boolean equals(PairNode<FirstType, SecondType> other) {
  189. boolean firstsEqual = this.getFirst().equals(other.getFirst());
  190. boolean secondsEqual = this.getSecond().equals(other.getSecond());
  191. return firstsEqual && secondsEqual;
  192. }
  193. /**
  194. * Returns whether this equals another object.
  195. * @param obj The object to determine equivalence with this.
  196. * @return True if obj is a Pair with elements equivalent to this, false otherwise.
  197. */
  198. public boolean equals(Object obj) {
  199. try {
  200. PairNode<FirstType, SecondType> other = (PairNode<FirstType, SecondType>) obj;
  201. return this.equals(other);
  202. }
  203. catch (ClassCastException e) {
  204. return false;
  205. }
  206. }
  207. /**
  208. * Returns a pair with the elements swapped.
  209. * @return A new Pair with the elements in reverse order.
  210. */
  211. public PairNode<SecondType, FirstType> getReverse() {
  212. return new PairNode<SecondType, FirstType>(this.getSecond(), this.getFirst());
  213. }
  214. /**
  215. * Returns a String representation of this Pair.
  216. * @return A string, "(X, Y)", where X is the first element and Y is the second element.
  217. */
  218. public String toString() {
  219. return this.first + ": " + this.second;
  220. }
  221. }
  222.  
  223. import java.io.*;
  224. import java.util.*;
  225. import java.nio.file.Files;
  226. import javax.swing.JFileChooser;
  227.  
  228. public class Tester{
  229. public static void main(String[] args){
  230. File file = readInFile();
  231. FTable tab = new FTable(file);
  232. tab.formTable();
  233. }
  234. /**
  235. * Asks user for file to be read from.
  236. * @return The file taken as input.
  237. */
  238. private static File readInFile(){
  239. final JFileChooser fc = new JFileChooser();
  240. int returnVal = fc.showOpenDialog(null);
  241. File file = fc.getSelectedFile();
  242. return file;
  243. }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement