mardi 24 février 2015

Piglatin Phrases Translator Not Working

My code shows no errors but when I type something in to get translated I get this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3 I don't know how to fix it so can you please tell me how. Here is the code:



import java.util.Scanner;
static String input = "";
static String translation = "";
public static void main(String[] args) {

//method to retrieve string, turn into pig latin, and return string
//breaks words into strings then pig latin
//pig latin =
//if word begins with consonant, first letter moved to end and added 'ay' to end
//if word begins with vowel, add 'way' to end


Scanner igLatinpay = new Scanner(System.in);
System.out.println("Please type something to turn into pig latin.");
input = igLatinpay.nextLine().toLowerCase();
System.out.println(translatedPhrase(input));
//System.out.println(pigLatin(translatedPhrase(input)));
}

// public static String[] pigWords(String big)
// {
// //breaks string into an array of words
// int k = 0;
// int c = 0;
// String[] myWords = new String[big.length()];
// for(int j = 0; j < big.length(); j+=1)
// {
// if(big.charAt(j) == ' ')
// {
// myWords[c] = big.substring(big.charAt(k), j);
// }
//
// k = j+1;
// c+=1;
// }
// return myWords;
// }

public static String translatedPhrase(String word)
{
//combines translated words into one string
String passDatLatin = "";
String[] words = input.split(" ");
for(int counter = 0; counter < words.length; counter +=1)
{
translation += pigLatin(words[counter]) + " ";
// passDatLatin = words[counter];
// System.out.print(pigLatin(passDatLatin) + " ");
}
return translation;
}

public static String search4Vowel(String piggy)
{
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
int end = piggy.length() - 1;//for horses, end is 5
for(int k = 0; k < piggy.length(); k += 1)//goes four times
{
for(int c= 0; c < vowels.length; c += 1)
{
if(piggy.charAt(k) == vowels[c])//goes through string pigsFly and
finds where there is vowel
{
piggy = piggy.substring(k, end) + piggy.charAt(0) + "ay";
}
else//if there are no vowels at all in the word
{
piggy += "ay";
}
}
}


return piggy;
}

public static String pigLatin(String pigsFly)
{
//TRANSLATES WORD
//have if statements to check beginning letter with else condition for consonant
//inside if's, do moving of letter and adding to end
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
String m = "";
int end = pigsFly.length() - 1;
for(int i = 0; i < vowels.length; i += 1)
{
//if word begins with vowel
if(pigsFly.charAt(0) == vowels[0] || pigsFly.charAt(0) == vowels[1] ||pigsFly.charAt(0) == vowels[2] || pigsFly.charAt(0) == vowels[3] ||pigsFly.charAt(0) == vowels[4])
{
m = pigsFly + "yay";
}
else//if word doesn't start with vowel
{
m = search4Vowel(pigsFly);
}
}
return m;//new pig latin word
}}


Thank you in advance.


Aucun commentaire:

Enregistrer un commentaire