This program is meant to import a text file and export the text file and the pig latin translations. The input is a single line text file that reads "It was a dark and stormy night" and the output needs to look as follows:
It ITWAY
was ASWAY
a AWAY
dark ARKDAY
and ANDWAY
stormy ORMYSTAY
night IGHTNAY
I only get the english words, not the pig latin words. When I initialize pigLatin, it prints whatever the initialization is, but I can't get the return value to actually update the variable. Please help!
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PigLatinTranslator {
public static void main(String[] args) throws FileNotFoundException {
File textFile = new File("/Users/juliansilvestre/eclipse-workspace/ProgrammingExercise4/src/ProgrammingExercise4TextFile.txt");
Scanner scan = new Scanner(textFile);
String line = scan.nextLine();
String[] wordsInFile = new String[7];
String pigLatin = "";
for (int i = 0; i < wordsInFile.length; i++) {
wordsInFile = line.split(" ");
translatePigLatin(wordsInFile[i], pigLatin);
System.out.println(wordsInFile[i] + "\t" + pigLatin);
}
}
public static String translatePigLatin(String english, String pigLatin) {
String upperCaseWord = english.toUpperCase();
int index = -1;
char ch;
for (int i = 0; i < upperCaseWord.length(); i++) {
ch = upperCaseWord.charAt(i);
if (isVowel(ch)) {
index = i;
break;
}
}
if (index == 0) {
return pigLatin = upperCaseWord + "WAY";
}
else {
String x = english.substring(index);
String y = english.substring(0, index);
return pigLatin = x + y + "AY";
}
}
public static Boolean isVowel(char ch) {
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
return true;
}
return false;
}
}