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

Thread: PigLatin translator help

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PigLatin translator help

    So here are the assignment instructions:

    For this assignment, your job is to create a program that reads in multiple lines of text, and then produces the translation of that text into the English language variant known as "pig latin".

    Pig latin works this way: if a word begins with a vowel (a-e-i-o-u), then "ay" is added to the end of the word (so "idle" -> "idleay", and "often" -> "oftenay"); on the other hand, if a word begins with a consonant, then the first letter is removed, and is placed at the end of the word, followed by "ay" (so "month" -> "onthmay", and "castle" -> "astlecay").

    Your program, then, should read in multiple lines of text, ending finally with two carriage returns. After the reading segment ends, your program should then print the pig latin translation of the input text. As a simplification, report the translation with no punctuation, and in all lower case.

    Below we give you the driver (the PigLatinTester class), and an input class called JInputer, which uses JOptionPane for input. Your job is to write the PigLatin class so that PigLatinTester works appropriately.

    public class PigLatinTester{
     
    public static void main(String[] args){
    JInputer r = new JInputer("pig latin!");
    r.multiInputs();
    r.reportPhrases();
    PigLatin p = new PigLatin(r.getPhrases());
    p.pigAll();
    p.pigReport();
    }
    }

    import javax.swing.JOptionPane;
     
    public class JInputer{
    private String cur = "";
    private String message; //JOptionPane caption
    private String[] phrases = new String[50];
    private int pos = 0;
     
    public JInputer(String msg){
    message = msg;
    }
     
    public JInputer(){
    }
     
    public String getInput(){
    cur = JOptionPane.showInputDialog(message);
    phrases[pos] = cur;
    pos++;
    return cur;
    }
     
    public void multiInputs(){
    String s = " ";
    while (s.length() > 0){
    s = getInput();
    }
    }
     
    public String[] getPhrases(){return phrases;}
     
    public void reportPhrases(){
    for (String s : phrases)
    if (s != null) System.out.println(s);
    }
     
    }

    My job is to write the PigLatin class so the driver works and this is what i got so far but it has compilation issues...
    import java.lang.*;
     
    public class PigLatin{
     
    public String[] phrases;
    public String result;
    public String t;
     
    public PigLatin(){}
     
     
    public String pigAll(){
    result="";
    StringTokenizer str;
    str = new StringTokenizer(phrases,"\t\n\r\f,.?!;: ");
    while(str.hasMoreTokens()){
    result += " ";
    result += translateTo(str.nextToken());
    } return result;
    }
     
    public String translateTo(String s){
    String s2 = "";
    for(int j=0; j< phrases.length; j++){
    s=phrases[j];
    s=s.toLowerCase();
    if((s.charAt(0)=='a' || s.charAt(0)=='e' || s.charAt(0)=='i' ||
    s.charAt(0)=='o' || s.charAt(0)=='u')){
    s2 = (s+"ay");
    }
    else{
    s2 = s.substring(1) + "ay"; 
    }
    } return s2;
    }
     
    public void pigReport(){
    System.out.println(pigAll());
    }
    }
    and i get these compilation errors:
    File: C:\Users\Korobkov\PigLatin.java [line: 16]
    Error: C:\Users\Korobkov\PigLatin.java:16: cannot find symbol
    symbol : constructor StringTokenizer(java.lang.String[],java.lang.String)
    location: class java.util.StringTokenizer
    File: C:\Users\Korobkov\PigLatinTester.java [line: 10]
    Error: C:\Users\Korobkov\PigLatinTester.java:10: cannot find symbol
    symbol : constructor PigLatin(java.lang.String[])
    location: class PigLatin

    What is the matter with the constructor and why is the
    StringTokenizer not being recognized...


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: PigLatin translator help

    Parameters to constructors and methods must match in number, type and order. StringTokenizer has 3 constructors:
    String
    String, String
    String, String, boolean

    Are you using any of those constructors? Obviously not since you got a compiler error. Same problem for your second error.

    P.S. The StringTokenizer class has been deprecated and you are encouraged to use the String.split method instead.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PigLatin translator help

    so i fixed the piglatin constructor but i still dont get the stringtokenizer one.
    import java.util.StringTokenizer;
    import java.lang.*;
     
    public class PigLatin{
     
      public String[] phrases;
      public String result;
      public String t;
     
      public PigLatin(String[] array){
        phrases = array;
      }
     
     
     
      public String pigAll(){
        result="";
        java.util.StringTokenizer str = new java.util.StringTokenizer(phrases,"\t\n\r\f,.?!;: ");
        while(str.hasMoreTokens()){
          result += " ";
          result += translateTo(str.nextToken());
        } return result;
      }
     
      public String translateTo(String s){
        String s2 = "";
        for(int j=0; j< phrases.length; j++){
          s=phrases[j];
          s=s.toLowerCase();
          if((s.charAt(0)=='a' || s.charAt(0)=='e' || s.charAt(0)=='i' ||
              s.charAt(0)=='o' || s.charAt(0)=='u')){
            s2 = (s+"ay");
          }
          else{
            s2 = s.substring(1) + "ay"; 
          }
        } return s2;
      }
     
      public void pigReport(){
        System.out.println(pigAll());
      }
    }
    1 error found:
    File: C:\Users\Korobkov\PigLatin.java [line: 18]
    Error: C:\Users\Korobkov\PigLatin.java:18: cannot find symbol
    symbol : constructor StringTokenizer(java.lang.String[],java.lang.String)
    location: class java.util.StringTokenizer

    my teacher is making us use stringtokenizer and from what examples i have seen there were no constructors made
    they just imported the java.util.*; and borrowed it from there. so what do i do to make this last error go away since im not supposed to make a separate class just to create stringtokenizer constructor and i dont think it will compile if i make the string tokenizer constructor within the piglatin class.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: PigLatin translator help

    Oh come on. What is the first parameter you are passing to the StringTokenizer constructor?

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

    Default Re: PigLatin translator help

    I am working on the same program for my Java class. I am stuck at the same point that Rusak is at. Is there a way to convert the String[] into a regular string? Or is there another way to find the solution.

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PigLatin translator help

    So the first part of the driver class allows the user to put in mulitiple inputs that are strings in an array. each separate input is a string in consecutive positions on the array until a blank string is added or a max of 50 positions is reached. The array is called phrases and is passed down to piglatin constructor and when the p.pigAll method runs the stringtokenizer has to go through each position of the array which has a string in it and tokenize it converting it to piglatin. This is the general idea which i have been following in my code but i have no clue as to where to go next from this error.
    import java.util.StringTokenizer;
    import java.lang.*;
     
    public class PigLatin{
     
      public String[] phrases;
      public String result;
      public String s;
     
      public PigLatin(String[] array){
        phrases = array;
      }
     
     
     
      public String pigAll(){
        result="";
        for(int j=0; j<phrases.length; j++){
          s=phrases[j];
          StringTokenizer str = new StringTokenizer(s,"\t\n\r\f,.?!;: ");
          while(str.hasMoreTokens()){
            result += " ";
            result += translateTo(str.nextToken());
          } 
        }return result;
      }
     
      public String translateTo(String s){
        String s2 = "";
        for(int j=0; j< phrases.length; j++){
          s=phrases[j];
          s=s.toLowerCase();
          if((s.charAt(0)=='a' || s.charAt(0)=='e' || s.charAt(0)=='i' ||
              s.charAt(0)=='o' || s.charAt(0)=='u')){
            s2 = (s+"ay");
          }
          else{
            s2 = s.substring(1) + "ay"; 
          }
        } return s2;
      }
     
      public void pigReport(){
        System.out.println(pigAll());
      }
    }
    java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at PigLatin.translateTo(PigLatin.java:33)
    at PigLatin.pigAll(PigLatin.java:23)
    at PigLatinTester.main(PigLatinTester.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)
    >
    So what now? what does it mean that the string index is out of range
    Last edited by Rusak; April 5th, 2011 at 07:33 PM.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: PigLatin translator help

    You haven't tried very hard. I posted the three constructors the StringTokenizer class has. You are trying to call the String array, String constructor which does not exist.

  8. #8
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PigLatin translator help

    I think i got it. It compiles but the dam thing wont work now lol.
    import java.util.StringTokenizer;
    import java.lang.*;
     
    public class PigLatin{
     
      public String[] phrases;
      public String result;
      public String s;
      public String token;
      StringTokenizer str;
     
      public PigLatin(String[] array){
        phrases = array;
      }
     
     
     
      public String pigAll(){
        result="";
        StringTokenizer str;
        for(int j=0; j<phrases.length; j++){
          s=phrases[j];
          str = new StringTokenizer(s,"\t\n\r\f,.?!;: ");
          while(str.hasMoreTokens()){
            result += " ";
            result += translateTo(str.nextToken());
          } 
        }  return result;
      }
     
      public String translateTo(String token){
        token=str.nextToken();
        token=token.toLowerCase();
        if((token.charAt(0)=='a' || token.charAt(0)=='e' || token.charAt(0)=='i' ||
            token.charAt(0)=='o' || token.charAt(0)=='u')){
          token = (token+"ay");
        }
        else{
          token = token.substring(1) + "ay"; 
        }
        return token;
      }
     
      public void pigReport(){
        System.out.println(pigAll());
      }
    }
    java.lang.NullPointerException
    at PigLatin.translateTo(PigLatin.java:32)
    at PigLatin.pigAll(PigLatin.java:26)
    at PigLatinTester.main(PigLatinTester.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)
    >
    So i used a for loop to go through each position in the array which contains a string and then pass that string to stringtokenizer which goes and while there are more tokens uses the translateTo method to translate each token... but it still errors.

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: PigLatin translator help

    On line 32 something is null. Time for you to debug your code. Add a print statement(s) before that line and print out all the variables to find out which is null. Then work out why it is null.

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: PigLatin translator help

    I just had a thought. If your phrases array has a length of 10 and you only add 3 phrases to it then the other 7 slots will be null. However your loop iterates over the entire array and you try and process the nulls as well.

  11. #11
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PigLatin translator help

    So i added a while statement that makes it continue as long as the string s is not null but now it wont work at all it just does the first part of the driver class where it reports the inputs but doesnt not print out the translation...
    import java.util.StringTokenizer;
    import java.lang.*;
     
    public class PigLatin{
     
      public String[] phrases;
      public String result;
      public String s;
      public String token;
      StringTokenizer str;
     
      public PigLatin(String[] array){
        phrases = array;
      }
     
      public String pigAll(){
        result="";
        StringTokenizer str;
        while(s!=null){
          for(int j=0; j<phrases.length; j++){
            str = new StringTokenizer(s,"\t\n\r\f,.?!;: ");
            s=phrases[j];
            while(str.hasMoreTokens()){
              result += " ";
              result += translateTo(str.nextToken());
            } 
          }  
        }return result;}
     
     
      public String translateTo(String token){
        token=str.nextToken();
        token=token.toLowerCase();
        if((token.charAt(0)=='a' || token.charAt(0)=='e' || token.charAt(0)=='i' ||
            token.charAt(0)=='o' || token.charAt(0)=='u')){
          token = (token+"ay");
        }
        else{
          token = token.substring(1) + "ay"; 
        }
        return token;
      }
     
      public void pigReport(){
        System.out.println(pigAll());
      }
    }

Similar Threads

  1. 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, 05:45 PM
  2. Piglatin Converter
    By jross21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:09 PM
  3. Help with Pig Latin Translator...
    By jchan in forum AWT / Java Swing
    Replies: 3
    Last Post: September 28th, 2009, 02:54 PM
  4. Pig Latin Translator
    By BMN901 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 17th, 2009, 03:31 AM

Tags for this Thread