简体   繁体   中英

Translate to Pig Latin in Java

I'm having trouble with this code that I made. My problem is only translating "friendly" into "iendlyfray". However when I tried to do execute this code, it would only translate to "riendlyfay". I changed the code in order to fix this but it would just end up into more errors in the other tests such. How do I change a word with multiple consonants in the first few characters of a word without having errors in the other tests? Thanks!

public static void main(String[] args) 
{
    boolean allCorrect = true;
    allCorrect = true;
    allCorrect &= testPigLatin("hello", "ellohay");
    allCorrect &= testPigLatin("ear", "earway");
    allCorrect &= testPigLatin("ugly", "uglyway");
    allCorrect &= testPigLatin("friendly", "riendlyfay");
    allCorrect &= testPigLatin("super", "upersay");
    allCorrect &= testPigLatin("young", "youngway");
    allCorrect &= testPigLatin("wonderful", "onderfulway");
    allCorrect &= testPigLatin("apple", "appleway");
    result(allCorrect, "translateToPigLatin"); } }

public static String translateToPigLatin(String word)
{
    String result = "";
    if(word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u") || word.startsWith("y") ) 
          return (word + "way");
 else { 
      char A = word.charAt(0);
       word = word.substring(1, word.length());
      return (word + A + "ay");
}
}
public static boolean testPigLatin(String strA, String answer)
{
    String result = translateToPigLatin(strA);

    if (result.equals(answer)) {
        System.out.println("CORRECT! \"" + strA + "\" is \"" + answer + "\"      in pigLatin.");
        return true;
    } else {
        System.out.println("Keep Trying! \"" + strA + "\" is \"" + answer + "\" in pigLatin but your method returned \"" + result + "\".");
        return false;
    }
}

I think if the word doesn't start with a vowel you need to find out where the first occurrence of a vowel is. So use word.indexOf("a"), etc for all the vowels. Then find out which index is the lowest. Take all the letters before the first vowel add it to the end and add ay.

This code would go in the else statement and it would look something like this (I don't know how y's work in pig latin so I'm not going to include them)

int a = word.indexOf("a");
int e = word.indexOf("e");
int i = word.indexOf("i");
int o = word.indexOf("o");
int u = word.indexOf("u");

String first = "";
//must make sure they aren't equal to -1
if(a!=-1 && a<e && a<i && a<o && a<u)
   first="a";
else if(e!=-1 && e<a && e<i && e<o && e<u)
   first="e"
else if(i!=-1 && i<a && i<e && i<o && i<u)
   first="i"
else if(o!=-1 && o<a && o<e && o<i && o<u)
   first="o"
else if(u!=-1 && u<a && u<e && u<i && u<o)
   first="u"

char A = word.substring(0,word.indexOf(first));
word = word.substring(word.indexOf(first), word.length());
return (word + A + "ay");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM