1 Replies - 838 Views - Last Post: 06 April 2011 - 01:02 PM Rate Topic: -----

#1 robcatbenimble   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-March 11

I'm have a problem with my code to convert english to pig latin

Posted 06 April 2011 - 12:35 PM

I have to convert text into pig latin. I have this so far but I don't know where to go from here

import java.util.*;

public class PigLatin
{
	public static void main(String[] args)
	{
		System.out.println("This program converts text into Pig Latin.");
		System.out.println();

		Scanner console = new Scanner(System.in);

		String phrase, word, pglWord;

		System.out.print("Enter a phrase ? ");
		phrase = console.nextLine();

		StringTokenizer st = new StringTokenizer(phrase);
		while (st.hasMoreTokens())
		{
			word = st.nextToken();

			pglWord = convertWordToPigLatin(word);

			System.out.print(pglWord + " ");

		}
		System.out.println();

		System.out.print("Bye now!");
	
	}
	public static String convertWordToPigLatin(String word)
	{
	   String rtVal = "Hi"; 
	  char letter;
	  letter =word.charAt(0);
	  
	  if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u');
	  {
		  rtVal = word + "-ay";	  
	  }
	  
	  
	  return rtVal;
       
	}
	}


This post has been edited by macosxnerd101: 06 April 2011 - 04:18 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I'm have a problem with my code to convert english to pig latin

#2 I ♣ Seals   User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 20-November 10

Re: I'm have a problem with my code to convert english to pig latin

Posted 06 April 2011 - 01:02 PM

Please use code tags, as suggested by the background text of the box you type your post into.

Here's some pseudocode for you to convert to Java. You've already done the bit before the "else" clause, so it's just a matter of doing the rest.

if word begins with a vowel
    return word + "ay"
else
    find the first vowel of the word
    move the letters before that to the end of the word
    return the result of this + "ay"
end if



Edit: Thinking about it, you don't need to bother with the condition at all - just apply the 'else' clause to each word regardless of what it starts with.

This post has been edited by I ♣ Seals: 06 April 2011 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1