1 Replies - 370 Views - Last Post: 15 November 2012 - 08:00 AM Rate Topic: -----

#1 amartin903  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-November 12

Help with a Pig Latin translator

Posted 15 November 2012 - 05:39 AM

Hi folks,

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

Is This A Good Question/Topic? 0
  • +

Replies To: Help with a Pig Latin translator

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Help with a Pig Latin translator

Posted 15 November 2012 - 08:00 AM

I don't of any way for java to understand monosyllabic words, becuase the compiler doesn't understand English, infact if you inputed the string "1lnsf!" it would be happy to ouput "lnsf!1ay"

What you should do, is parse a list like so:
http://www.one-syllable-words.com/

If the word is found in that list, then you know to use your special rule.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1