Translating text into Pig Latin with Java

Published on . Takes less than 1 minute to read.

I have a beginner level Java exam coming up tomorrow and I'm doing some last-minute studying. Here's a primitive Java program I wrote to translate English text into Pig Latin.

It has some obvious shortcomings like lacking validity checks and inability to handle symbols.

The program is text based. It asks for a file, reads it, loops over individual words and outputs and writes the resulting translation.

Stuffy.java
import java.util.*;
import java.io.*;

/*
 * Converts a file of English text into Pig Latin.
 * @author Ando Roots 2011
 */
public class Stuffy {

    /*
     * Main controller, asks for a file and calls other methods.
     */
    public static void main(String[] args) {
        TextIO.put("Enter the path for the file (example: /home/john/file.txt): ");
        String file = TextIO.getlnString();
        String translation = translateIntoPigLatin(readFile(file));
        TextIO.put(translation);
        writeFile(translation, "PigLatinOutput.txt");
        TextIO.put("nThe translation has been saved to the current directory as PigLatinOutput.txt");

    }

    /*
     * Read a textfile and return it's contents
     * http://www.kodejava.org/examples/28.html
     */
    private static String readFile(String filename) {
        File file = new File(filename);
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null) {
                contents.append(text).append(
                        System.getProperty("line.separator"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return contents.toString();
    }

    /*
     * Write a string to file
     * http://www.roseindia.net/java/beginners/WriteTextFileExample.shtml
     */
     public static void writeFile(String input, String filename){
         try {Writer output = null;
            File file = new File(filename);
            output = new BufferedWriter(new FileWriter(file));
            output.write(input);
            output.close();
         } catch (IOException e) {
             TextIO.put("I/O error");
         }
            }

    /*
     * Takes a string of English text as a input and returns it as Pig Latin
     *
     * @param input A string of English text
     *
     * @return String Pig Latin
     */
    private static String translateIntoPigLatin(String input) {
        String output = "";

        // Break the long string into words (spaces as seperators)
        StringTokenizer st = new StringTokenizer(input);

        // Iterate over every word
        while (st.hasMoreTokens()) {
            output += translateWord(st.nextToken()) + " ";
        }
        return output;
    }

    /*
     * Translates a single word into Pig Latin
     *
     * @param word A String to be translated
     *
     * @return A Pig Latin string
     */
    private static String translateWord(String word) {

        // Check to see if the first letter is a vowel or a consonant
        if (isVowel(word.substring(0, 1))) {
            word = word + "-ay";
        } else {
            // If it's a word beginning with a consonant we need to know the
            // position of the first vowel.
            int vowel_position = 0;
            for (int i = 0; i < word.length(); i++) {
                String letter = word.substring(i, i + 1);

                if (isVowel(letter)) {
                    vowel_position = i;
                    break;
                }
            }       

            // Do the actual conversion to PL
            if (vowel_position != word.length()) {
            word = word.substring(vowel_position, word.length()) + "-"
                    + word.substring(0, vowel_position) + "ay";
            }
            // If the word begins with a consonant and contains no vowels?
        }
        // Leave only alphanumeric since the program can't handle anything else
        word = word.replaceAll("[^a-zA-Z0-9]", "");
        return word;
    }

    /*
     * Check if a character is a vowel
     */
    private static boolean isVowel(String input) {
        String vowels[] = { "a", "e", "i", "o", "u" };
        if (Arrays.asList(vowels).contains(input.toLowerCase())) {
            return true;
        } else {
            return false;
        }
    }
}

View gist on GitHub

Projectsjavapig latinprogramtranslate