What is a java pig latin translator

573    Asked by AnishaDalal in Java , Asked on Oct 10, 2022

 Over the last few days I created this Pig Latin Translator just for fun. I would really appreciate it if anybody could give suggestions of how to improve the code and make the program more efficient.

Here is the GitHub link for the project.


For anybody who doesn't know Pig Latin, feel free to look at the README, which explains what Pig Latin is, and the rules of the language. The code is in PigLatin.java.


    /*  -------------------- Program Information --------------------
    Name Of Program: PigLatin.java
    Date of Creation: 7/3/2018
    Name of Author(s): Agnik Banerjee
    Version of Java: 1.8.0_171
    Created with: IntelliJ IDEA 2017.3.5 Community Edition
    -------------------- Program Information --------------------  */
import java.util.Scanner;
public class PigLatin {
    private static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.print("Enter one or more words that you would like to translate to Pig Latin: ");
        final String userInput = scan.nextLine();
        scan.close();
        String[] word = userInput.split(" "); // Splits the string into an array of words
        String output = "";
        for (int i = 0; i < word>            String pigLatinWord = translateWord(word[i]); // Translates each word individually
            output += pigLatinWord + " "; // Joins the translated word back into the output
        }
        System.out.println("Original Word(s): " + userInput);
        System.out.println("Translation: " + output + "n");
    }
    public static String translateWord(String word) {
        String lowerCaseWord = word.toLowerCase();
        int pos = -1; // Position of first vowel
        char ch;
        // This for loop finds the index of the first vowel in the word
        for (int i = 0; i < lowerCaseWord>            ch = lowerCaseWord.charAt(i);
            if (isVowel(ch)) {
                pos = i;
                break;
            }
        }
        if (pos == 0) {
            // Translating word if the first character is a vowel (Rule 3)
            return lowerCaseWord + "yay"; // Adding "yay" to the end of string (can also be "way" or just "ay")
        } else {
            // Translating word if the first character(s) are consonants (Rule 1 and 2)
            String a = lowerCaseWord.substring(pos); // Extracting all characters in the word beginning from the 1st vowel
            String b = lowerCaseWord.substring(0, pos); // Extracting all characters located before the first vowel
            return a + b + "ay"; // Adding "ay" at the end of the extracted words after joining them.
        }
    }
    // This method checks if the character passed is a vowel (the letter "y" is counted as a vowel in this context)
    public static Boolean isVowel(char ch) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
            return true;
        }
        return false;
    }
}


Answered by Ankesh Kumar

I would suggest the following points to make this programme more object-oriented: Try using a collection object provided by Java pig latin instead of using array

String[] word = userInput.split(" ");
could be
List words = Arrays.asList(userInput.split(" "));
or using external library Guava
List words = Splitter.on(" ").splitToList(userInput);

Avoid comments where you can.

In this case I can see you can easily avoid comment if you can wrap up the particular code snippet with the meaningful name. For instance You could take the user input and breaking it in a list in separate class and call the class from your main

List words = new UserInputHandler().getInputAsList();
Try to use foreach loop over for loop:
You can easily use the foreach loop to iterate over words
for (String word : words) {
    String pigLatinWord = translateWord(word); // Translates each word individually
    output += pigLatinWord + " "; // Joins the translated word back into the output
}


Your Answer

Interviews

Parent Categories