java - Pig Latin translator -- finding consonant clusters. -
i'm making pig latin translator translates user's input pig latin. have figured out when word starts vowel , when doesn't putting first letter in middle. however, when comes consonant clusters (the group of characters before first vowel in word), can't figure out how lump them own variable. i'm using loop scan letter first variable, , trying clump strings it's own variable put middle of word.
here's code have far:
import java.util.scanner; public class mission4 { public static void main(string[] args) { scanner in = new scanner (system.in); system.out.println("please enter word convert piglatin:"); string userinput = in.nextline(); int firstv = 0; char firstch = character.tolowercase(userinput.charat(0)); { if (firstch == 'a' || firstch == 'e' || firstch == 'i' || firstch == 'o' || firstch == 'u') //if userinput starts vowel { string pigtalk = userinput + "ay"; system.out.println(pigtalk); // adding 'ay' end of input system.out.println("enter word convert piglatin, otherise press \"q\" exit."); userinput = in.nextline(); } else //if userinput doesn't begin vowel { (int = 0; < firstv; i++) { char ch = userinput.charat(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'); { firstv = character.substring(ch); } } string firstcluster = userinput.substring(0,firstv.length); string secondcluster = userinput.substring(firstv.length,userinput.length()); system.out.println(secondcluster + firstcluster + "ay"); //printing out piglatin system.out.println("enter word, or type \"q\" exit program."); userinput = in.nextline(); } } while (!userinput.equals("q")); //giving user exit option } }
can offer advice? appreciated.
first of all, try organize code smaller functions, forming logical units. makes easier both , others understand program doing, , doing wrong.
several mistakes in program:
firstv
set 0, , loop stops immediately:for (int = 0; < firstv; i++)
- the loop broken (doesn't compile)
what loop needs iterate second character until finds vowel.
here's corrected main logic extracted function, , helper function:
public string topiglatin(string word) { if (isvowel(word.charat(0))) { return word + "ay"; // actually, wikipedia says should append "yay", not "ay" } (int = 1; < word.length(); i++) { if (isvowel(word.charat(i))) { return word.substring(i) + word.substring(0, i) + "ay"; } } return word + "ay"; } public boolean isvowel(char c) { c = character.tolowercase(c); return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; }
Comments
Post a Comment