Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Pig Latin Translator

    I have been off for school for two weeks due to a broken femur bone (yes I know, it hurts) and I need some extra java help. My teacher said that I have a program due on Monday (aka pig latin translator) and for some reason I keep getting a few errors.

    Java Code:
    /* * 09/09/2011
     * A pig latin translator used by the Department of Defense that converts English words to pig latin based on a set of strict rules.
     */
    import java.util.*;
    public class IgpayAtinlay {
        
        public static void main(String []args){//main
            
            Scanner Keyboard = new Scanner(System.in);
            
            System.out.print("English: ");
            String input = Keyboard.nextLine();//grabs user input for the english that needs to be translated.
            
            System.out.print("Translation: " + phraseToPig(input));//prints translation to the screen
            //calls "phraseToPig()" function
                
            
        }
        
        public static String phraseToPig(String phrase){
            
           String current_word, fin = "";
            
            String[] word = phrase.split("");
            
            for(int i=0; i<=phrase.length()-1; i++)
            {
                current_word=word[i];
                fin=fin+wordToPig(current_word);[B]
                
                
            }[/B]
            
            return fin;
            
        }
        
        public static String wordToPig (String english){
            
            String end, start, fin = "";
            int index=0;
            
            
            /*if(isCapitalized(english)==true){
                lowerCase(english);
                //upperCase(current_word);
            }*/
            
            index=hasAVowel(english);
            
            
            //if no vowels
            if(index==-1){
                fin = english+"ay";
            }
            
            //else if begins with vowel
            if(index==0){
                fin=english+"yay";
            }
            
            //else has vowel and doesn't start with vowel
            if(index!=-1 && index>0){
            start = english.substring(0, index);
            end = english.substring(index, english.length()-index);
            fin = end+start+"ay";
            }
            
            
            return fin;
        
            
        }
        
        public static int hasAVowel(String word){
            
            char letter=0;
            int index=0, num=0;
            for(int i=0; i<word.length(); i++){
                letter=word.charAt(i);
                if(letter=='a' || letter=='i' || letter=='o' || letter=='e' || letter=='u' || letter=='A' || letter=='I' || letter=='O' || letter=='E' || letter=='U'){
                    index=i;
                    i=word.length();
                    num=1;
                }
                
            }
            if(num==0){
                index=-1;
            }
            
            return index;
            
            
            
        }
        
        
        
        
        /*public static boolean isCapitalized (String word){
            
            if(word.charAt(0) >= 'A' && word.charAt(0) <= 'Z'){
                return true;
                
            }
            else
                return false;
            
        }
        
        public static String lowerCase(String word){
            
            word=word.toLowerCase();
            return word;
        }*/
        
        
        
        
        /*public static String upperCase(String word){
            String temp, new_word;
            
            temp=word.substring(1);
            temp=temp.toUpperCase();
            
            new_word=temp;
            
            for(int i=1; i<word.length(); i++){
            new_word=new_word+word[i];
            
            
            }
            */
        
    
    
    }
    This is the output when I debug:
    English: Hi this is a test
    Translation: nullay Hay iyay ay tay hay iyay say ay iyay say ay ayay ay tay eyay say


    Then I tried to do another test with just my name:

    English: Daniel
    Translation: nullay Day ayay nay iyay eyay

    EDIT: I did ANOTHER test:

    English: Daniel Goldberg
    Translation: ayDayayaynayiyayeyaylay ayGayoyaylaydaybayeyayray

    I fixed the spacing however it still is only grabbing letters rather than words.




    So now I realize that its separating letters rather than words....Is there something wrong with my .split function when I do that?

    Does anyone know what is wrong?

    Thanks for your support.
    Last edited by danthegreat; 09-10-2011 at 04:21 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Pig Latin Translator

    Your posted examples don't say what is wrong and how the output should look.

    Is there something wrong with my .split function
    Write a small test program that uses split and prints out the results.
    The java.util.Arrays toString() method is useful for displaying the contents of arrays.

  3. #3
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Pig Latin Translator

    Ok, so I did a test:
    Java Code:
    import java.util.*;public class test2 {
        public static void main(String[] args){
            
            String word="Hello this is daniel";
                
            
        
            String[] word2 = word.split("");
            
            for(int i=0; i<=word.length()-1; i++)
            {
                System.out.println(word2[i]);
                
                
                
    }
    This is the output:


    H
    e
    l
    l
    o

    t
    h
    i
    s

    i
    s

    d
    a
    n
    i
    e


    I notice two things:
    1) The "L" is gone (don't know why)
    2) Grabs letters and not words

    The original example that my teacher gave which successfully grabbed words was:
    String[] word2 = word.split(" "); << As you can see, there is a space between the quotes....On my test, this is what I got:

    Hello
    this
    is
    daniel
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at test2.main(test2.java:13)


    That output above is PERFECT except for the fact that it has the error above. Then I debugged my real program and received this:

    English: Hi this is a test
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at IgpayAtinlay.phraseToPig(IgpayAtinlay.java:31)
    at IgpayAtinlay.main(IgpayAtinlay.java:17)

    It does not even display results, is something is up with the array?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Pig Latin Translator

    ArrayIndexOutOfBoundsException: 4
    What value are you using to end the for loop?

    Your variable names could be confusing you:
    you use word for a sentence
    and word2 a for an array of words

  5. #5
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Pig Latin Translator

    I use word2 and word in my small program to just test to see if it works.

    for(int i=0;i<=phrase.length-1;i++)
    ^
    the loop ends once i reaches the end of the phrase.

    So if I have "Hello this is daniel"

    i=0, Hello
    i=1, this
    i=2, is
    i=3, daniel

    The loop should end before reaching 4...There is no word at the 4th index.
    This seems to be happening when i enter in any phrase. It says the computer says index Z is nowhere to be found but index Z doesn't exist and the computer seems to be looking too far by 1.

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Pig Latin Translator

    A hint. insert next code in your for{} loop

    Java Code:
    System.out.println(word.length());
    to see how long is your String

  7. #7
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Pig Latin Translator

    I get 20..- confused -

    1 h
    2 e
    3 l
    4 l
    5 o
    6
    7 t
    8 h
    9 i
    10 s
    11
    12 i
    13 s
    14
    15 d
    16 a
    17 n
    18 i
    19 e
    20 l

    output:
    20
    Hello
    this
    is
    daniel
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at test2.main(test2.java:15)

    does that mean my loop is going 20 times???? it should be going 3 times since i used .split
    Last edited by danthegreat; 09-10-2011 at 07:11 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Pig Latin Translator

    java.lang.ArrayIndexOutOfBoundsException: 4
    What value are you using to end the for loop?
    Last edited by Norm; 09-10-2011 at 07:27 PM.

  9. #9
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Pig Latin Translator

    Yes but you are doing for{} loop for length of String word that have 20 characters, what you should do is to loop for only 3 or 4 times. You need to loop as many times as your array has elements...

  10. #10
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Pig Latin Translator

    [I]
    Quote Originally Posted by milovan View Post
    Yes but you are doing for{} loop for length of String word that have 20 characters, what you should do is to loop for only 3 or 4 times. You need to loop as many times as your array has elements...
    Thank you for your response! I switched up my code to my actual project:
    Java Code:
    public static String phraseToPig(String phrase){        
            String current_word, fin = "";
            
            String[] word = phrase.split(" ");
            
            for(int i=0; i<=word.length-1; i++)
            {
                current_word=word[I];
                fin=fin+wordToPig(current_word) + " ";
                
                
            }
            
            return fin;
            
    }[/I]


    I realized the for{} loop was just examining the phrase variable rather than the word variable.

    Now my output becomes:
    English: Hi this is a test
    Translation: Hay thay isyay ayay estay

    This is great except for 1 thing, how did the 'i' in "Hi" suddenly "disappear"?
    EDIT: Also i just noticed this turned into "thay" or is the "isyay" split from "This" or is "isyay" from "is" from the original statement? Hmm..this is tough.

    This is my code when it comes to changing the words:
    Java Code:
    index=hasAVowel(english);        
            
            //if no vowels
            if(index==-1){
                fin = english+"ay";
            }
            
            //else if begins with vowel
            if(index==0){
                fin=english+"yay";
            }
            
            //else has vowel and doesn't start with vowel
    [B]        if(index!=-1 && index>0){[/B]
    [B]        start = english.substring(0, index);[/B]
    [B]        end = english.substring(index, english.length()-index);[/B]
    [B]        fin = end+start+"ay";[/B]
            }
            
            
    return fin;
    Shouldn't the bolded statements trigger when the word "Hi" is entered because it does not start with a vowel but has a vowel?


    EDIT: I tried a new statement --
    English: hello i is daniel
    Translation: ellhay iyay isyay anieday

    The real translation should be
    ellohay iyay isyay anielday

    There is a missing character from some words O.o



    Last edited by danthegreat; 09-10-2011 at 07:42 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Pig Latin Translator

    Shouldn't the bolded statements trigger
    Add some printlns to show the values of variables as they are set and changed to see where your logic is not working.

  12. #12
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Pig Latin Translator

    I solved basically everything above however this is now my output:
    English: All I ask for are some sharks with some Freakin Laser beams attached to their heads.
    Translation: Allyay Iyay askyay orfay areyay omesay arkshay ithway omesay eakiFray aserLay eamsbay attachedyay otay eithay eads.hay

    the word sharks should be "arksshay" and the word heads needs to be "eadshay." and eithay needs to be "eirthay". There must be something wrong when I am grabbing words and switching them around......in these lines:

    Java Code:
            //else has vowel and doesn't start with vowel
    Java Code:
    [B]      if(index!=-1 && index>0){
           start = english.substring(0, index);
           end = english.substring(index, (english.length()-index)+1);
           fin = end+start+"ay";
    [/B][B]       }[/B]
    I even tried to physically read off my code by hand:

    Sharks

    start="Sh"
    end="arks"

    fin = "arks" + "Sh" + "ay"

    My code should work completely

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Pig Latin Translator

    There must be something wrong
    Debug your code by adding printlns to show what is going on. Print out the String and the values of all the variables used to do the substrings etc.

  14. #14
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Pig Latin Translator

    Any progress?
    what are your start and end variables when you print them?

  15. #15
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Pig Latin Translator

    I tested numerous times:
    (ALL BOLDED ARE INCORRECT)

    with the phrase:
    end = english.substring(index, (english.length()-index)+1);


    English: Sharks
    Sh
    ark
    Translation: arkShay

    English: Daniel
    D
    aniel
    Translation: anielDay

    English: Truman
    Tr
    uma
    Translation: umaTray

    English: Helmet Head
    H
    elmet
    H
    ead
    Translation: elmetHay eadHay

    NOW, with the phrase:
    end = english.substring(index, (english.length()-index)+2);

    English: Sharks
    Sh
    arks
    Translation: arksShay

    English: Daniel
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
    at java.lang.String.substring(String.java:1934)
    at IgpayAtinlay.wordToPig(IgpayAtinlay.java:68)
    at IgpayAtinlay.phraseToPig(IgpayAtinlay.java:32)
    at IgpayAtinlay.main(IgpayAtinlay.java:17)

    English: Truman
    Tr
    uman
    Translation: umanTray

    English: Helmet Head
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
    at java.lang.String.substring(String.java:1934)
    at IgpayAtinlay.wordToPig(IgpayAtinlay.java:68)
    at IgpayAtinlay.phraseToPig(IgpayAtinlay.java:32)
    at IgpayAtinlay.main(IgpayAtinlay.java:17)


    It doesn't make sense.. could it be something with the order of vowels on a word?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Pig Latin Translator

    StringIndexOutOfBoundsException: String index out of range: 7
    at java.lang.String.substring(String.java:1934)
    at IgpayAtinlay.wordToPig(IgpayAtinlay.java:68)
    Look at line 68 and see how/why the index to substring is past the end of the String

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Pig Latin Translator

    Your IDE must have a way to show which line you have selected or found in a search or what line your cursor is on or something like that.
    How many substring calls do you have about 3/4 of the way down on a page

  18. #18
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Pig Latin Translator

    Line 68: end = english.substring(index, (english.length()-index)+1);

    English: Sharks
    2 (index)
    Sh (start)
    ark (end: lacks an s)
    Translation: arkShay

    It should work! I've tested it numerous times! How can the computer fail?

  19. #19
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Pig Latin Translator

    And just one question about your logic. Do you need to use
    Java Code:
    substring(int start,int end)
    I think that
    Java Code:
    substring(int start)
    is just enough for your needs...
    It seems like
    Java Code:
    english.length()-index)+1
    is causing the problem...
    Last edited by milovan; 09-10-2011 at 08:33 PM.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Pig Latin Translator

    How can the computer fail?
    It VERRRRRRRRRRRy rarely does.
    It usually does EXACTLY what you tell it to do.

    You should be printing out the value of these:
    english
    index,
    (english.length()-index)+1)

Page 1 of 2 12 LastLast

Similar Threads

  1. applicaton for nokia S60 devices(Translator), need help
    By Basit781 in forum CLDC and MIDP
    Replies: 0
    Last Post: 05-18-2010, 03:42 PM
  2. java spanish translator needed for jni
    By Nicholas Jordan in forum Jobs Offered
    Replies: 5
    Last Post: 08-11-2009, 03:43 PM
  3. Custom Font Translator
    By Nerdopolis in forum New To Java
    Replies: 3
    Last Post: 04-18-2009, 03:50 AM
  4. Translator hashtable
    By editor35 in forum New To Java
    Replies: 1
    Last Post: 01-11-2009, 02:02 AM
  5. Java multilanguage translator
    By mnprakash in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-05-2009, 08:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •