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 2 of 2

Thread: Piglatin Converter

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Piglatin Converter

    Hello all,
    I am in an intro to Java class right now and I have an assignment due later today that converts a body of text to piglatin.

    We are given a driver class which cannot be changed.

    import java.util.*;
    public class PigDriver{
      public static void main(String[] args){
       Scanner scan = new Scanner(System.in);
       String t = " ";
       Piglatin p =new Piglatin();
       while(t.length() > 0){
         t = scan.nextLine();
         t = t.toLowerCase();
         p.pigConvert(t);
       }
       p.pigReport();
      }
    }

    and I have the following code written:

    import java.util.StringTokenizer;
    import java.lang.String;
     
    public class Piglatin{
     
      StringBuffer cookies;
     
     
      public Piglatin(){
      }
     
            public StringBuffer pigConvert(String m)
            {
                    StringBuffer result = new StringBuffer();
                    String delimiters = " ,.:;()[]{}\"'!@#$%^&*";
                    StringTokenizer lexer;
                    String token;
     
                    lexer = new StringTokenizer(m, delimiters, true);
     
                    while (lexer.hasMoreTokens())
                    {
                            token = lexer.nextToken();
                            if (delimiters.indexOf(token) > -1)
                                    result.append(token);
                            else
                                    result.append(transformToken(token));
                    }
     
     
     
                    cookies = result;
                    return cookies;
     
            }
     
     
            public static String transformToken(String original)
            {
                    StringBuffer result = new StringBuffer();
                    String vowels = "aeiou";
     
                    String vowelRule = "way";
                    String consonantRule = "ay";
     
                    if (vowels.indexOf(original.substring(0, 1).toLowerCase()) > -1)
                    {
                            result.append(original);
                            result.append(vowelRule);
                    }
                    else
                    {
                            result.append(original.substring(1));
                            result.append(original.substring(0, 1));
                            result.append(consonantRule);
                    }
     
                    return result.toString();
            }
     
            public void pigReport(){
              System.out.println("something");
            }
     
     
      }

    The problem is that I can't print the converted text to the console. Normally I would just try to write a print statement into the method that's converting it, but I must follow the driver class. I wanted to make a stringbuffer (in this case cookies) that would exist outside of the pigConvert method, but when I try to print this it prints nothing. Any help would be greatly appreciated.

    Thanks so much!


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Piglatin Converter

    Hello jross21,

    Do you still need help with this? Sorry for the late reply!!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.