Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Pig Latin translator

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Pig Latin translator

    I have been trying to get my piglatin translator to translate more than one word but dont know how to. Please help, this following code is what I have and it works for one word only at a time.

    import java.util.Scanner;
    /**
    * Write a description of class pigLatin here.
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    public class pigLatin
    {

    public static void main(String[] args)
    {
    Scanner myScan = new Scanner(System.in);
    String word= "";
    String vowelA="a";
    String vowelE="e";
    String vowelI="i";
    String vowelO="o";
    String vowelU="u";

    System.out.println("Enter a word to be converted to pig latin");
    word = myScan.nextLine();




    if (word.startsWith(vowelA) || word.startsWith (vowelE)|| word.startsWith (vowelI)|| word.startsWith (vowelO) || word.startsWith (vowelU))

    {
    System.out.println(word+ "way");
    }
    else
    {
    System.out.println (word.substring(1) + word.charAt(0)+("ay"));
    }
    }


    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pig Latin translator

    How do you want to change it?
    Allow a user to enter more than one word on a line?
    Allow a user to enter more than one line with one word on a line? That will require a way for the user to tell the program that he's done.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Pig Latin translator

    I would like to have the user enter a phrase and have each word in the phrase converted to piglatin, however what i have now only converts the first word in the phrase each time, I am really new to Java (three days to be exact) and dont know how to change this

    import java.util.Scanner;
    /**
     * Write a description of class pigLatin here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class pigLatin
    {
     
        public static void main(String[] args)
        {
            Scanner myScan = new Scanner(System.in);
            String word= "";
            String vowelA="a";
            String vowelE="e";
            String vowelI="i";
            String vowelO="o";
            String vowelU="u";
     
            System.out.println("Enter a word to be converted to pig latin");
                word = myScan.nextLine();
     
     
     
     
            if (word.startsWith(vowelA) || word.startsWith (vowelE)|| word.startsWith (vowelI)|| word.startsWith (vowelO) || word.startsWith (vowelU))
     
            {
                System.out.println(word+ "way");
            }
            else 
            {
                System.out.println (word.substring(1) + word.charAt(0)+("ay"));
            }
        }
     
     
        }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pig Latin translator

    There are a couple of ways to parse a line with multiple words separated by spaces so you get the words one at a time.
    The Scanner class has a constructor that will take a String in its constructor and then allow you to use the next methods to get the tokens/words one at a time from that Scanner object. You would use a hasNext method to detect when there are no more tokens available.

    Another way is the String class's split() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    geabus1043 (March 4th, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Pig Latin translator

    thanks, I figured out how to work it

Similar Threads

  1. Replies: 5
    Last Post: August 20th, 2012, 01:01 AM
  2. Whats wrong with this Pig Latin code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 6th, 2011, 01:57 PM
  3. having problems with my pig latin translator (the translating portion)
    By WontonSoup in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2011, 06:45 PM
  4. Help with Pig Latin Translator...
    By jchan in forum AWT / Java Swing
    Replies: 3
    Last Post: September 28th, 2009, 02:54 PM
  5. Pig Latin Translator
    By BMN901 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 17th, 2009, 03:31 AM