I've been creating a Pig Latin translator for a group project for university (we don't have to actually make a translator, just manipulate a string in any way we'd like, and I chose this).
The input into my translator is a Latin prayer, the first two lines of which are:
credo in unum deum
patrem omnipotentem
I've created my translator with the following code:
public static void pigLatinify(String fname) throws IOException
{
File file = new File("projectdata.txt");
try
{
Scanner scan1 = new Scanner(file);
while (scan1.hasNextLine())
{
Scanner scan2 = new Scanner(scan1.nextLine());
boolean test2;
while (test2 = scan2.hasNext())
{
String s = scan2.next();
char firstLetter = s.charAt(0);
if (firstLetter=='a' || firstLetter=='i' || firstLetter=='o' || firstLetter=='e' ||
firstLetter=='u' || firstLetter=='A' || firstLetter=='I' || firstLetter=='O' ||
firstLetter=='E' || firstLetter=='U')
{
String output = s + "hay" + " ";
System.out.print(output);
}
else
{
String restOfWord = s.substring(1);
String output = restOfWord + firstLetter + "ay" + " ";
System.out.print(output);
}
}
System.out.println("");
}
scan1.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
It outputs the entire prayer well, with the following output for the first two lines:
redocay inhay unumhay eumday
atrempay omnipotentemhay
However, in true Pig Latin, monosyllabic words stay the same and have "hay" added to the end, so it becomes ithay, egg becomes egghay, but multi syllabic words have way added to the end instead, so archery becomes "archeryway" and ending becomes "endingway".
Is there a way for Java (and the scanner class I'm using) to detect if a word is monosyllabic?
At this point I'll also point out I'm only a beginner programmer, so if there is but it is extraordinarily complicated, feel free to just say that!!
Thanks, Andrew

New Topic/Question
Reply



MultiQuote




|