Reputation: 21
This is for a java pig latin translator homework assignment. I'm new to java and having a great deal of difficulty with this problem. When I input the file to read and write to i'm getting an array index out of bounds error. At this point i'm not even sure if the translator part of the code will work properly. Any help with correcting the error and the translator code would be much appreciated.
while(true){
System.out.println("Enter a text file to translate to Pig Latin. Quit to end.");
fileName = keyboard.nextLine();
if (fileName.equalsIgnoreCase("quit")){
break; //End program.
}
System.out.println("Enter a file to write the translated file to.");
outputFile = keyboard.nextLine();
if (outputFile.equalsIgnoreCase("quit")){
break; //End program.
}
fileToTranslate = new File(fileName);
try {
fileReader = new Scanner(fileToTranslate);
} catch (FileNotFoundException ex) {
//Logger.getLogger(PigLatinTranslatorKukuruda.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("File does not exist. Enter a new file.");
}
while(fileReader.hasNext()){
word = fileReader.next();
}
fileReader.close();
System.out.println(translator(word));
}
}
public static String translator(String string){
String[] wordsToTranslate = string.split(" ");
String word;
String translatedString = " ";
int first = 0;
for(int i = 0; i <= wordsToTranslate.length-1; i++){
word = wordsToTranslate[1];
if ((word.charAt(0) == 'a') || (word.charAt(0) == 'e') || (word.charAt(0) == 'i') ||
(word.charAt(0) == 'o') || (word.charAt(0) == 'u') || (word.charAt(0) == 'A') ||
(word.charAt(0) == 'E') || (word.charAt(0) == 'I') || (word.charAt(0) == 'O') ||
(word.charAt(0) == 'U')){
translatedString = word + "yay";
}
else{
for(int j = 0; j <= word.length()-1; j++){
if ((word.charAt(j) == 'a') || (word.charAt(j) == 'e') || (word.charAt(j) == 'i') ||
(word.charAt(j) == 'o') || (word.charAt(j) == 'u') ||
(word.charAt(j) == 'A') || (word.charAt(j) == 'E') ||
(word.charAt(j) == 'I') || (word.charAt(j) == 'O') ||
(word.charAt(j) == 'U')){
if (first == 0){
translatedString = word.substring(j) + word.substring(0, j) + "ay";
first = 1;
}
}
}
}
}
return translatedString;
}
Right now im just getting the error and not sure what to do. This is supposed to write to a file but I was just trying to ouput it right now to see if it works but i'm not getting that far because of the error. thanks
Upvotes: 1
Views: 297
Reputation: 36
Anis R. is correct, but to answer your next question (how to translate each word):
After reading in each word, you should add it to any array (or ArrayList, or similar). You can then pass the array of all the words to your translator class.
If you want every word to be read into one string, as it seems that you've tried to do in your code, you'll need to change
word = fileReader.next();
to
word += fileReader.next();
this will append the next word into the string.
Edit: Thanks Anis R, it has to be
word += " " + fileReader.next();
To correctly include the spaces.
To answer your followup question about ArrayLists: Create an ArrayList for words like this:
ArrayList<String> list = new ArrayList<String>();
Then, inside your while(fileReader.hasNext())
loop, change the code to list.add(fileReader.next());
This will add each word (without spaces) to the list - each word will be a separate string, and a unique index in the list. You can then iterate through the list (you can find the length using list.size()
) to translate each word. Since spaces aren't included, when you print out your translated results you'll want to print spaces between each word.
Upvotes: 1
Reputation: 6922
Consider the following part of your code:
while(fileReader.hasNext()){
word = fileReader.next();
}
fileReader.close();
System.out.println(translator(word));
Your scanner is reading word by word (because of .next()
).
And inside your while loop, you are overwriting the value of word
every iteration, so at the end of the loop, word
will be equal to the last word of your file.
Therefore your call to translate(word)
will only pass the last word as a parameter. That is, one word. Therefore word = wordsToTranslate[1];
will definitely throw an IndexOutOfBounds
exception as there is no second word.
Plus, I believe you meant to use word = wordsToTranslate[i];
inside your loop?
Upvotes: 2