Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final commit for the pig-latin project from cmauer69. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/edu/htc/piglatin/FileCompareUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package edu.htc.piglatin;

import java.io.FileInputStream;
import java.io.IOException;

/**
* This is a utility class to compare files.
* It is based on an example from the textbook.
*/
/**
* Created by clifford.mauer on 4/4/2016.
*/
public class FileCompareUtil {

public static boolean compare(String path1, String path2) throws IOException {

boolean same = true;
try (FileInputStream f1 = new FileInputStream(path1);
FileInputStream f2 = new FileInputStream(path2)) {
System.out.println("Total file size to read (in bytes) : "
+ f1.available());
System.out.println("Total file size to read (in bytes) : "
+ f2.available());

int first, second;
do {
first = f1.read();
second = f2.read();
if (first != second) {
same = false;
break;
}
} while (first != -1 && second != -1);

if (first != second){
same = false;
}
}
return same;

}
}
45 changes: 45 additions & 0 deletions src/edu/htc/piglatin/FileParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package edu.htc.piglatin;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
* Created by clifford.mauer on 4/4/2016.
*/
public abstract class FileParser {

/**
* This class will read sentences from a file and store them in an ArrayList.
* The file must have one sentence per line.
*
* Punctuation at the end of the sentence will not be removed.
*/
public static ArrayList<String> parseFile(String fileName) throws IOException {

// Complete this method so that the test below in the main method is correct.
ArrayList<String> strReturn = new ArrayList<>();
boolean blnExit = false;

BufferedReader reader = new BufferedReader(new FileReader(fileName));
try {
while (!blnExit) {
String line = reader.readLine();
if (line.trim().length() != 0) {
strReturn.add(line);
System.out.println(line);
blnExit = false;
}
else {
if (line == null){
blnExit = true;
}
}
}
} finally {
reader.close();
return strReturn;
}
}
}
31 changes: 31 additions & 0 deletions src/edu/htc/piglatin/ListFileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package edu.htc.piglatin;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
* Created by clifford.mauer on 4/4/2016.
*/
public class ListFileWriter {

public static void writeToFile(ArrayList<String> list, String fileName) throws IOException {

// Complete this method so that the test below in the main method is correct.
boolean blnExit = false;

FileWriter fw = new FileWriter(fileName);
try {
for (String str : list) {
if ( str.trim().length() != 0){
fw.write(str.trim() + "\r\n");
}
}
fw.close();
}
catch (IOException e){
System.out.println("I/O Error: " + e);
}
}

}
16 changes: 8 additions & 8 deletions src/edu/htc/piglatin/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package edu.htc.piglatin;

import edu.htc.file.FileCompareUtil;
import edu.htc.file.FileParser;
import edu.htc.file.ListFileWriter;
import edu.htc.piglatin.FileCompareUtil;
import edu.htc.piglatin.FileParser;
import edu.htc.piglatin.ListFileWriter;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -11,15 +11,15 @@

public class Main {

public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException, NoSentenceException {

String current = new File( "." ).getCanonicalPath();

String origFileEnglish = ClassLoader.getSystemResource("data/sampleText.txt").getFile();
String outputFilePigLatin = current + "/sampleText_PigLatin.txt";
String outputFileEnglish = current + "/samplePigLatin_English.txt";

String origFilePigLatin = ClassLoader.getSystemResource("data/samplePigLatin.txt").getFile();
String outputFileEnglish = current + "/samplePigLatin_English.txt ";
String outputFilePigLatin = current + "/sampleText_PigLatin.txt";

try {

Expand All @@ -43,7 +43,7 @@ public static void main(String[] args) throws IOException {
}
}

public static void translateFileToPigLatin(String inFilePath, String outFilePath) {
public static void translateFileToPigLatin(String inFilePath, String outFilePath) throws NoSentenceException {
// Read File
ArrayList<String> sentences = null;
try {
Expand All @@ -69,7 +69,7 @@ public static void translateFileToPigLatin(String inFilePath, String outFilePath
}
}

public static void translateFileFromPigLatin(String inFilePath, String outFilePath) {
public static void translateFileFromPigLatin(String inFilePath, String outFilePath) throws NoSentenceException {
// Read File
ArrayList<String> sentences = null;
try {
Expand Down
16 changes: 16 additions & 0 deletions src/edu/htc/piglatin/PigLatinExceptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.htc.piglatin;

/**
* Created by clifford.mauer on 4/11/2016.
*/
public class PigLatinExceptions extends Exception {
public PigLatinExceptions(String message) {
super(message);
}
}

class NoSentenceException extends Exception {
public NoSentenceException(String message) {
super(message);
}
}
Loading