Question: Topic: Computer Science (College Intro) | Java Programming Hey, I'm doing a programming assignment, and I need a bit of help. I've accounted for two
Topic: Computer Science (College Intro) | Java Programming Hey, I'm doing a programming assignment, and I need a bit of help. I've accounted for two of the possible three tests, but the last test is giving me trouble: Here's the problem:
Write a piece of code that converts a user-input to pig Latin: Rules of Pig Latin: First, let's make sure you understand the rules of the Pig Latin language:
Words that begin with consonant sounds ll letters before the initial vowel are placed at the end of the word followed by the word "ay" which is added as a suffix at the end of the word. Example: hello -> ellohay
Words that begin with multiple consonants. The consonants are put to the end of the word with the suffix "ay." Example: child -> ildchay
Words that begin with vowels (a, e, i, o, u, y) - Words that start with vowels are handled in that the 1st character vowel is moved to the end of the word along with adding the suffix "way". Example: okay-> kayoway
Words that have no vowels - Words that have no listed vowels (a, e, i, o, u, y) should be left alone. For example, the word "CNN" should be left as is when converted to PigLatin since it does not contain any of our listed vowels.
This is my code:
import java.util.Scanner; public class PigLatin { private static final String VOWELS = "AEIOU"; //main() - manager method for pig latin translator public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String phrase = ""; //continue to get phrases to convert to pig latin until user quits while (!phrase.equals("quit")) { phrase = getInput(); //make sure user didn't enter an empty string if (phrase.length() == 0) { System.out.println("Input phrase required"); } else { //convert the phrase String pigLatin = convertToPiglatin(phrase); displayPigLatin(pigLatin); } } } //display phrase public static void displayPigLatin(String phrase) { System.out.println(phrase + " in pigLatin is ... "); System.out.println("==> " + phrase.toUpperCase()); System.out.println(); } //converts to pigLatin and returns public static String convertToPiglatin(String phrase) { String pigLatin = ""; //holds a converted word String convertedPhrase = ""; //holds a converted phrase Scanner wordScanner = new Scanner(phrase); //get the next word and convert it to pigLatin String word = wordScanner.next(); pigLatin = convertWord(word, false); //attach converted word to converted phrase convertedPhrase = convertedPhrase + " " + pigLatin; //should loop to convert all the words in the phrase //convertToPigLatin(convertedPhrase); //This isn't working, so Bedha just comments it return convertedPhrase; } //helper method for convertToPigLatin() //converts a single word passed as a parameter to pigLatin and return public static String convertWord(String word, boolean done) { String pigLatin = ""; //holds the converted word done = false; //sentinel for stopping loop int i = 0; //tracks position in string //go through word character by character looking for first vowel while (!done) { String x = word.substring(i, 1); if (isVowel(x) == 1) { pigLatin = word.substring(i) + " " + word.substring(0, i) + "AY"; done = true; } i++; //move to next character in word } return pigLatin; } //reads in phrase and return public static String getInput() { Scanner console = new Scanner(System.in); //prompt for input phrase System.out.print("Enter the word or phrase to be converted. "); System.out.println("Type quit to end the program:"); String line = console.next(); //read input return line; } //accepts letter and checks if vowel public static int isVowel(String x) { //loop through all possible vowels and see if there's a match for (int i = 0; i <= VOWELS.length(); i++) if (VOWELS.contains(x)) { //yes, this is a vowel return 1; } return -1; //went through all vowels and determined this is not a vowel } }
You cannot use arrays or replaceAll but rather simply while loops, for loops, if statements). I have figured out how to accomplish it for the first two sample results but not the last result with spaces.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
