Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.nio.file.Files;
- import javax.swing.JFileChooser;
- /**
- * Creates a frequency table of characters found in a file
- * @author Matthew Ferland
- */
- public class FTable{
- //List of characters and frequencies in this list.
- private ArrayList<PairNode<Character, Integer>> list;
- //File to be read from
- private File file;
- /**
- * Constructor for this class. Creates an empty ArrayList.
- * @param file File that the FTable is to be read from.
- */
- public FTable(File file){
- this.list = new ArrayList<PairNode<Character, Integer>>();
- this.file = file;
- formTable();
- outputToFile();
- System.out.println(this);
- }
- /**
- * Creates a FTable from file, and outputs frequency to new file.
- */
- public void formTable(){
- String string = fileToString();
- this.frequencyAnalysis(string);
- }
- /**
- * Turns a string into a series of pairs of characters and their frequency
- * @param string String to be converted.
- */
- private void frequencyAnalysis(String string){
- String charsTaken = "";
- for(int i = 0; i < string.length(); i++){
- if(charsTaken.contains(String.valueOf(string.charAt(i)))){
- continue;
- }
- else{
- char c = string.charAt(i);
- charsTaken += c;
- int count = 0;
- for(int j = i; j < string.length(); j++){
- if(string.charAt(j) == c){
- count++;
- }
- }
- PairNode<Character, Integer> pair = new PairNode<Character,Integer>(c, count);
- list.add(pair);
- }
- }
- }
- /**
- * Outputs this into a file of the user's choice.
- */
- private void outputToFile(){
- System.out.print("Would you like to send the frequency to a specific output file?");
- System.out.println(" If so, type 'y' or 'yes'. Else, type 'n' or 'no'.");
- String s = "";
- final Scanner sc = new Scanner(System.in);
- while (!s.equals("y") && !s.equals("yes")){
- s = sc.nextLine();
- if(s.equals("n") || s.equals("no")) return;
- }
- final JFileChooser fc = new JFileChooser();
- fc.setSelectedFile(new File("frequency.txt"));
- int returnVal = fc.showSaveDialog(null);
- File file = fc.getSelectedFile();
- try{
- FileWriter writer = new FileWriter(file);
- String out = this.toString();
- writer.append(out);
- writer.flush();
- writer.close();
- }
- catch(IOException e){
- System.out.println("Cancelled and aborted");
- }
- }
- /**
- * Takes the file passed into this by the instructor into a string.
- * @return A string version of the file to be read in.
- */
- private String fileToString(){
- List<String> strings = new ArrayList<String>();
- try{
- strings = Files.readAllLines(this.file.toPath());
- }
- catch(IOException e){}
- //String that holds string version of file.
- String string = "";
- //Takes the list of strings and turns it into a single string
- for(int i = 0; i < strings.size(); i++){
- if(i < strings.size() - 1){
- string += strings.get(i) + "\n";
- }
- else{
- string += strings.get(i);
- }
- }
- return string;
- }
- /**
- * Returns a string frequency table.
- * @return A string version of this.
- */
- public String toString(){
- String string = "";
- for(int i = 0; i < this.list.size(); i++){
- if(this.list.get(i).getFirst() == '\n'){
- string += "\\n: " + this.list.get(i).getSecond() + "\n";
- }
- else if(this.list.get(i).getFirst() == ' '){
- string += "(space): " + this.list.get(i).getSecond() + "\n";
- }
- else if(this.list.get(i).getFirst() == '\t'){
- string += "\\t: " + this.list.get(i).getSecond() + "\n";
- }
- else{
- string += this.list.get(i) + "\n";
- }
- }
- return string;
- }
- }
- /**
- * Represents a Pair of values.
- * @author Matthew Ferland
- */
- public class PairNode<FirstType, SecondType> {
- //the first element in the pair
- private FirstType first;
- //the second element in the pair
- private SecondType second;
- //The left child of this node
- private PairNode<FirstType, SecondType> left;
- //The right child of this node
- private PairNode<FirstType, SecondType> right;
- /**
- * Class constructor
- * @param first The first element in this Pair
- * @param second The second element in this Pair
- */
- public PairNode(FirstType first, SecondType second) {
- this.first = first;
- this.second = second;
- this.left = null;
- this.right = null;
- }
- /**
- * Returns the first element.
- * @return The first element in this.
- */
- public FirstType getFirst() {
- return this.first;
- }
- /**
- * Returns the second element.
- * @return The second element in this.
- */
- public SecondType getSecond() {
- return this.second;
- }
- /**
- * Sets the first element.
- * @param newFirst The value to set the first element to.
- */
- public void setFirst(FirstType newFirst) {
- this.first = newFirst;
- }
- /**
- * Sets the first element.
- * @param newFirst The value to set the first element to.
- */
- public void setSecond(SecondType newSecond) {
- this.second = newSecond;
- }
- /**
- * Tests whether two Pairs are equal.
- * @param other Another pair that might be equivalent to this.
- * @return True if both the first element of this equals the first element of other
- and the second element of this equals the second element of other, false otherwise.
- */
- public boolean equals(PairNode<FirstType, SecondType> other) {
- boolean firstsEqual = this.getFirst().equals(other.getFirst());
- boolean secondsEqual = this.getSecond().equals(other.getSecond());
- return firstsEqual && secondsEqual;
- }
- /**
- * Returns whether this equals another object.
- * @param obj The object to determine equivalence with this.
- * @return True if obj is a Pair with elements equivalent to this, false otherwise.
- */
- public boolean equals(Object obj) {
- try {
- PairNode<FirstType, SecondType> other = (PairNode<FirstType, SecondType>) obj;
- return this.equals(other);
- }
- catch (ClassCastException e) {
- return false;
- }
- }
- /**
- * Returns a pair with the elements swapped.
- * @return A new Pair with the elements in reverse order.
- */
- public PairNode<SecondType, FirstType> getReverse() {
- return new PairNode<SecondType, FirstType>(this.getSecond(), this.getFirst());
- }
- /**
- * Returns a String representation of this Pair.
- * @return A string, "(X, Y)", where X is the first element and Y is the second element.
- */
- public String toString() {
- return this.first + ": " + this.second;
- }
- }
- import java.io.*;
- import java.util.*;
- import java.nio.file.Files;
- import javax.swing.JFileChooser;
- public class Tester{
- public static void main(String[] args){
- File file = readInFile();
- FTable tab = new FTable(file);
- tab.formTable();
- }
- /**
- * Asks user for file to be read from.
- * @return The file taken as input.
- */
- private static File readInFile(){
- final JFileChooser fc = new JFileChooser();
- int returnVal = fc.showOpenDialog(null);
- File file = fc.getSelectedFile();
- return file;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement