Home > Enterprise >  Is there a way to call a method to convert each word to pig latin and put those words into a sentenc
Is there a way to call a method to convert each word to pig latin and put those words into a sentenc

Time:11-05

This is the code that I have written so far that translates a single word into Pig Latin. It tells the user what the program does and asks for the user to input a word. Then the word they typed in is translated into the Pig Latin version. My problem is that I'm not sure how to translate a whole sentence into Pig Latin and display it on one line, or how to return the converted phrase in a method as a string.

Ex. the word typed in apple is shown again with quotes around it followed by in Pig Latin is apple-way

This program will convert an English word into Pig Latin.

Please enter a word ==> apple

"apple"
  in Pig Latin is
"apple-way"
import java.util.*;

public class PigLatin2 {

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println("This program will convert an English word into Pig Latin.\n");
        String word = readWord(console);
        System.out.println();
        convertWord(word);
        console.close();
    }

    public static String readWord(Scanner console) {
        System.out.print("Please enter a word ==> ");
        String word = console.next();
        return word;
    }

    public static String convertWord(String englishWord) {
        String pigLatinWord;

        if (isVowel(englishWord.charAt(0)))
            pigLatinWord = englishWord   "-way";

        else if (englishWord.startsWith("Th") || englishWord.startsWith("th") || englishWord.startsWith("TH")
                || englishWord.startsWith("tH")) {

            pigLatinWord = (englishWord.substring(2)   "-"   englishWord.substring(0, 2)   "ay");

        } else

            pigLatinWord = (englishWord.substring(1)   "-"   englishWord.charAt(0)   "ay");
        printResult(englishWord, pigLatinWord);
        return pigLatinWord;
    }

    public static boolean isVowel(char c) {
        if (c == 'A' || c == 'a' || c == 'E' || c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U'
                || c == 'u') {
            return true;
        } else
            return false;
    }

    public static void printResult(String englishWord, String pigLatinWord) {
        System.out.println("\""   englishWord   "\"");
        System.out.println("  in Pig Latin is");
        System.out.println("\""   pigLatinWord   "\"");
    }

}

A new string method has to be made called convertPhrase and has a string parameter called englishPhrase.

I tried to put in a new method with a while loop but got this instead as the output:

This program will convert an English phrase into Pig Latin.

Please enter a phrase ==> Geiser is the best teacher

"Geiser"
  in Pig Latin is
"eiser-Gay"
"is"
  in Pig Latin is
"is-way"
"the"
  in Pig Latin is
"e-thay"
"best"
  in Pig Latin is
"est-bay"
"teacher"
  in Pig Latin is
"eacher-tay"
"Geiser is the best teacher"
  in Pig Latin is
"eiser is the best teacher-Gay"
import java.util.*;

public class PigLatin2 {

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println("This program will convert an English phrase into Pig Latin.\n");
        String phrase = readWord(console);
        System.out.println();
        convertPhrase(phrase);
        console.close();
    }

    public static String readWord(Scanner console) {
        System.out.print("Please enter a phrase ==> ");
        String phrase = console.nextLine();
        return phrase;
    }
    
    public static String convertPhrase(String englishPhrase) {
        Scanner line = new Scanner(englishPhrase);
        while (line.hasNext()) {
            String grab = line.next();
            convertWord(grab);
        }
        return convertWord(englishPhrase);
    }

    public static String convertWord(String englishWord) {
        String pigLatinWord;

        if (isVowel(englishWord.charAt(0)))
            pigLatinWord = englishWord   "-way";

        else if (englishWord.startsWith("Th") || englishWord.startsWith("th") || englishWord.startsWith("TH")
                || englishWord.startsWith("tH")) {

            pigLatinWord = (englishWord.substring(2)   "-"   englishWord.substring(0, 2)   "ay");

        } else

            pigLatinWord = (englishWord.substring(1)   "-"   englishWord.charAt(0)   "ay");
        printResult(englishWord, pigLatinWord);
        return pigLatinWord;
    }

    public static boolean isVowel(char c) {
        if (c == 'A' || c == 'a' || c == 'E' || c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U'
                || c == 'u') {
            return true;
        } else
            return false;
    }

    public static void printResult(String englishPhrase, String pigLatinPhrase) {
        System.out.println("\""   englishPhrase   "\"");
        System.out.println("  in Pig Latin is");
        System.out.println("\""   pigLatinPhrase   "\"");
    }

}

CodePudding user response:

Split the input on the space character to get an array of words. Then iterate over each word in the array and call the convertWord method and combine the results into a new String with space as a delimiter.

public static String convertPhrase(String englishPhrase) {
   String[] words = englishPhrase.split(" ");
   List<String> convertedWords = new ArrayList<>();
   for (String word : words) {
       convertedWords.add(convertWord(word));
   }
   return String.join(" ", convertedWords);
}

CodePudding user response:

You can express word patterns (words that start with vowels or words that start with non-vowels) in regular expressions. You can do a conversion for each pattern matched for each word in the phrase.

This method convertPhrase() can also be used as convertWord().

static final Pattern WORD = Pattern.compile(
    "(?:[aeiou]|([^aeiou\\s] ))(\\w*)", Pattern.CASE_INSENSITIVE);

public static String convertPhrase(String englishPhrase) {
    return WORD.matcher(englishPhrase).replaceAll(m -> 
        m.group(1) != null
            ? m.group(2)   "-"   m.group(1)   "ay"
            : m.group()   "-way");
} 

test:

public static void main(String[] args) {
    System.out.println(convertPhrase("Geiser is the best teacher"));
}

output:

eiser-Gay is-way e-thay est-bay eacher-tay
  • Related