Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java application that acts as a one-word Pig Latin translator. Your prog

ID: 3737472 • Letter: W

Question

Write a java application that acts as a one-word Pig Latin translator. Your program should:

ask the user to enter a word

convert the entered word into proper Pig Latin translation

print the translated word on the screen

I have the following code, I just need it to be fixed so that the following condition is met. (currently whenever "xylophone" is entered, the program prints "ophonexylay", when it should print "ylophonexay"

Rule #2:
Words that start with a consonant should have all consonant letters up to the first vowel moved to the end of the word and then "ay" is appended.
For example, the word "chair" translates into "airchay"

'Y' is considered to be vowel in this rule, so the word "xylophone" translates into "ylophonexay"

Here is my code:

import java.util.*;

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

System.out.println("Please enter a word: ");
String userWord = kbd.next();

System.out.println("Your word translated into pig latin is: "+translate(userWord));
}
  
public static String translate(String pigLatin) {
  
pigLatin = pigLatin.toLowerCase();
pigLatin = pigLatin.trim();
  
// Words that start with a vowel (A, E, I, O, U) should simply have the characters
// "way" appended to the end of the word.
switch(pigLatin.charAt(0))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return pigLatin + "way";
}
// Words that start with a consonant should have all consonant letters up to the
// first vowel moved to the end of the word and then "ay" is appended.
//string newWord = "";
for(int i = 0; i < pigLatin.length(); i++)
{
switch(pigLatin.charAt(i))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return pigLatin.substring(i) + pigLatin.substring(0, i) + "ay";
}
}
return pigLatin + "ay";
}
}

Explanation / Answer

PigLatin.java

import java.util.*;

public class PigLatin {

  

public static Scanner kbd;

  

public static void main(String[] args) {

  

kbd = new Scanner(System.in);

System.out.println("Please enter a word: ");

String userWord = kbd.next();

System.out.println("Your word translated into pig latin is: "+translate(userWord));

}

  

public static String translate(String pigLatin) {

  

pigLatin = pigLatin.toLowerCase();

pigLatin = pigLatin.trim();

  

// Words that start with a vowel (A, E, I, O, U) should simply have the characters

// "way" appended to the end of the word.

switch(pigLatin.charAt(0))

{

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

return pigLatin + "way";

}

// Words that start with a consonant should have all consonant letters up to the

// first vowel moved to the end of the word and then "ay" is appended.

//string newWord = "";

String newStr = "";

if(pigLatin.length()>1){

if(pigLatin.charAt(1) =='h'){

newStr = pigLatin.substring(2);

newStr = newStr+"-"+pigLatin.substring(0,2)+"ay";

}

else {

newStr = pigLatin.substring(1);

newStr = newStr+pigLatin.charAt(0)+"ay";

}

return newStr;

}

else{

return pigLatin;

}

}

}

Output:

Please enter a word:
xylophone
Your word translated into pig latin is: ylophonexay

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote