Convert English to Pig Latin in Java

I am tutoring students in two classes this semester at a local community college: Java II as well as PHP and MySQL. The new semester has just started and the first homework assignment is Java II is to convert an English sentence to Pig Latin.

The rules for the Pig Latin conversion are simpler than the rules for Pig Latin you will find in Wikipedia. The only rule is to remove the first character from each word, append it to the end of the word, and add the letters “ay”.

e.g. “hello world” -> “ellohay orldway”

Java Homework Assignment

Write an application that encodes English language phrases into pig Latin. Pig Latin is a form of coded language. There are many different ways to form pig Latin phrases. For simplicity, use the following algorithm:

To form a pig Latin phrase from English language phrase, tokenized the phrase into words with string method split. To translate each English word into pig Latin word, place the first letter of the English word at the end of the word and add the letters”ay.” Thus, the word “jump” becomes “umpjay” and the word “computer” becomes “omputercay.” Blanks between words remain as blanks. Assume the following: The English phrase consists of words separated by blanks, there are no punctuation marks and all words have two or more letters. Method printLatinWord should display each word. Each token is passed to method printLatinWord to print the pig Latin word. Enable the user to input the sentence. Then display the converted sentence.

Solution

The students and I broke this problem into 3 parts.

  • Read the English sentence from the console (stdin).
  • Split the sentence into words (tokens).
  • Convert each English Word (token) to Pig Latin.

Reading the English Sentence from Stdin using Java

We can read a line from stdin in Java using Scanner. Scanner has a method called nextLine which can read the entire English sentence at one time. We decided on the following Java code.

Scanner scanner = new Scanner(System.in);

System.out.print("Enter sentence: ");
String sentence = scanner.nextLine();

Splitting a String into Space-Separated Words using Java

The String class in Java has a convenient method called split which can be used to split a String into space-separated words. The split method returns an array of String, which will be the individuals words in the English sentence. We can then pass these individual words, called tokens in the instructions, to the printLatinWord method for conversion and display.

String[] tokens = sentence.split(" ");

for (String token : tokens) {
    printLatinWord(token);
}

Convert English Word to Pig Latin using Java

The String class in Java also has a convenient method called substring which allows one to get a substring of a String given a beginning an optional ending index. We can use this method to re-position the characters in each English word (token) to form the Pig Latin word.

private static void printLatinWord(String token) {
    String pigLatinWord = token.substring(1) + token.substring(0, 1) + "ay";
    System.out.printf("%s ", pigLatinWord);
}

Java Program to Convert English Sentence to Pig Latin

The complete Java program built by the students and I using the tools they have learned in Java 2 thus far is as follows.

import java.util.Scanner;

public class Main {
    private static void printLatinWord(String token) {
        String pigLatinWord = token.substring(1) + token.substring(0, 1) + "ay";
        System.out.printf("%s ", pigLatinWord);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter sentence: ");
        String sentence = scanner.nextLine();

        String[] tokens = sentence.split(" ");

        for (String token : tokens) {
            printLatinWord(token);
        }
    }
}

Contents