Write a java application that encodes English-language phrases into Pig Latin. T
ID: 3565781 • Letter: W
Question
Write a java application that encodes English-language phrases into Pig Latin. Tokenize 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 letters "ay". Method printLatinWord should display each word. Each token is passed to method rintLatinWord to print the pig Latin word. Enable user to input the sentence and use more than once. This is what i have so far
import java.util.Scanner;
import java.util.StringTokenizer;
public class PigLatin {
public static void main(String[] args) {
Scanner input; input = new Scanner(System.in);
final String vowels = "aeiouAEIOU";
System.out.println("Please enter a phrase.");
String phrase = input.nextLine();
for (int num=0;num<100;num++) {
String beforVowel = "";
int replace = 0;
while (replace < phrase.length() && !vowels.contains("" + phrase.charAt(replace))) {
beforVowel += phrase.charAt(replace); replace++; }
System.out.println(phrase.substring(replace) + beforVowel + "ay");
System.out.println("Please enter a phrase.");
phrase = input.nextLine();
String[] printLatinWord = phrase.split(" ");
for (String token : printLatinWord) System.out.println(token); } } }
Explanation / Answer
package vijay;
import java.util.Scanner;
import java.util.StringTokenizer;
public class PigLatin {
public static void main(String[] args) {
Scanner input;
input = new Scanner(System.in);
while(true){
System.out.println("Please enter a phrase.");
String phrase = input.nextLine();
new PigLatin().printLatinWord(phrase);
}
}
public String printLatinWord(String phrase)
{
final String vowels = "aeiouAEIOU";
for (int num=0;num<100;num++) {
String beforVowel = "";
int replace = 0;
while (replace < phrase.length() && !vowels.contains("" + phrase.charAt(replace))) {
beforVowel += phrase.charAt(replace); replace++; }
//System.out.println(phrase.substring(replace) + beforVowel + "ay");
//System.out.println("Please enter a phrase.");
//phrase = input.nextLine();
String[] printLatinWord = phrase.split(" ");
for (String token : printLatinWord)
System.out.println(token.substring(1,token.length())+token.charAt(0)+ "ay");
break;
}
return "";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.