java - Translator, only accepts one word, why not the rest -
java - Translator, only accepts one word, why not the rest -
i trying create programme translates english language piglatin. have of components, if come in in more 1 word, translates first word. issue , how prepare it.
public class piglatin{ public static string translate(string phrase){ string [] returnarray=phrase.split(" "); string [] translatearray=new string [returnarray.length]; for(int i=0;i<returnarray.length;i++){ translatearray[i]=translateword(returnarray[i]); } homecoming stringlib.join(translatearray, " ");//translated array } public static string translateword(string word) { string tword=word; int indexvowel=indexoffirstvowel(tword); if(indexoffirstvowel(tword)==0){ tword=tword+"yay"; } else { tword=tword.substring(indexoffirstvowel(tword),tword.length())+tword.substring(0,indexoffirstvowel(tword))+"ay"; } homecoming tword; } public static int indexoffirstvowel(string word) {//check first vowel string vowels = "aeiouy"; string loweredword=word.tolowercase(); (int index=0;index<loweredword.length();index++){ if(vowels.contains(string.valueof(loweredword.charat(index)))){ homecoming index; } } homecoming -1; } public static void main(string [] args){ scanner inp=new scanner(system.in); system.out.println("please come in phrase:"); string trans=translate(inp.next()); system.out.println("here phrase in pig latin."); system.out.println(trans); }
string.join translates array string , here code
public class stringlib { public static string join(string[] strs, string sep) { string joined = ""; if (strs.length > 0) { joined = strs[0]; (int = 1; < strs.length; i++) { joined = joined + sep + strs[i]; } } homecoming joined; }
change line in main
method
string trans = translate(inp.next());
into
string trans = translate(inp.nextline());
java arrays string sorting
Comments
Post a Comment