This program is supposed to read a sentence of user input and translate it to piglatin by appending "yay" to a word beginning with a vowell and for words beginning with consonants or consonant blends ("br", "tw", "st" etc..) moving the consonant/blend to the end then adding an "ay". My version of the program does not produce any output. But it does prompt me to type in a sentence (which is in the do loop of my main method). Any help, please
main:
//PigLatin.java import java.util.Scanner; public class PigLatin { public static void main (String[] args) { String sentence, result, another; Scanner scan = new Scanner (System.in); do { System.out.println (); System.out.println ("Enter a sentence (no punctuation):"); sentence = scan.nextLine(); System.out.println (); result = PigLatinTranslator.translate (sentence); System.out.println ("That sentence in Pig Latin is:"); System.out.println (result); System.out.println(); System.out.print ("Translate another sentence (y/n)?"); another = scan.nextLine(); } while (another.equalsIgnoreCase("y")); } }
And the called method:
//PigLatinTranslator.java import java.util.Scanner; public class PigLatinTranslator { //translates a sentence of words into piglatin public static String translate (String sentence) { String result = ""; sentence = sentence.toLowerCase(); Scanner scan = new Scanner (sentence); while (scan.hasNext()); { result += translateWord (scan.next()); result += " "; } return result; } //translates ONE word into piglatin. Using the language rules of //that language. private static String translateWord (String word) { String result = ""; if (beginsWithVowel (word)) result = word + "yay"; else if (beginsWithBlend (word)) result = word.substring(2) + word.substring(0,2) + "ay"; else result = word.substring(1) + word.charAt(0) + "ay"; return result; } //Finds out if the word begins with a vowell private static boolean beginsWithVowel (String word) { String vowels = "aeiou"; char letter = word.charAt(0); return (vowels.indexOf (letter) != -1); } //Finds out if the word begins with any of the two character //consonant blends private static boolean beginsWithBlend (String word) { return ( word.startsWith ("bl") || word.startsWith ("sc") || word.startsWith ("br") || word.startsWith ("sh") || word.startsWith ("ch") || word.startsWith ("sk") || word.startsWith ("cl") || word.startsWith ("sl") || word.startsWith ("cr") || word.startsWith ("sn") || word.startsWith ("dr") || word.startsWith ("sm") || word.startsWith ("dw") || word.startsWith ("sp") || word.startsWith ("fl") || word.startsWith ("sq") || word.startsWith ("fr") || word.startsWith ("st") || word.startsWith ("gl") || word.startsWith ("sw") || word.startsWith ("gr") || word.startsWith ("th") || word.startsWith ("kl") || word.startsWith ("tr") || word.startsWith ("ph") || word.startsWith ("tw") || word.startsWith ("pl") || word.startsWith ("wh") || word.startsWith ("pr") || word.startsWith ("wr") ); } }