package piglatinTranslation;
import java.util.Scanner;
import java.util.Vector;
public class PigLatin {
public static void main (String[]args)
{
Scanner Input = new Scanner(System.in);
int words = 1;
Vector<Object> spacesList = new Vector<Object>();
Vector<Object> translatedWordList = new Vector<Object>();
String Phrase = Input.nextLine();
Input.close();
//Compares each character in the string 'Phrase'
//If a character is found as an empty space, it adds to the word count and saves the index of the space in a vector
for(int v = 0; v < Phrase.length(); v++)
{
char temp = Phrase.charAt(v);
String tempString = Character.toString(temp);
if(tempString.equals(" "))
{
words++;
spacesList.add(v);
}
}
//Takes each item in the vector (an integer for the index of each space within the sting)
// and creates a substring for each word, putting it though the translation method
// The translated word is added to a vector of strings
for(int v = 0; v < words; v++)
{
if(v == 0)
{
int subStrEnd = (int) spacesList.get(v);
Phrase.substring(1, subStrEnd);
translatedWordList.add(Translate.translateWord(Phrase));
}
else
{
int subStrStart = (int) spacesList.get(v - 1);
int subStrEnd = (int) spacesList.get(v);
Phrase.substring(subStrStart, subStrEnd);
translatedWordList.add(Translate.translateWord(Phrase));
}
}
//Takes each string in the vector and combines them into one string to be returned to the user
for(int v = 0; v < words; v++)
{
Phrase.concat((String) translatedWordList.get(v));
}
System.out.println(Phrase);
}
}
A user should be able to input a string, and have it replied back to them translated through pig Latin. For example: Input: Hello Output: Ellohay
Input: Hello, how are you Output: ellohay, owhay areyay ouyay
I keep getting an out of bounds error at line 43.
That line should, in theory use the temporary variable 'v', and use that as a pointer to take the stored integer, the index of a space, to be used to separate the string into word substrings.
Your vector is being populated correctly, it's just that when there are n words there are only n - 1 spaces. So you would only want to loop up to words - 1. Generally in these types of situations though, you would just use spacesList.size() and you don't need to use the words variable at all.