Results 1 to 20 of 23
Thread: Pig Latin Translator
- 09-10-2011, 04:03 PM #1
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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.
This is the output when I debug: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]; } */ }
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.
- 09-10-2011, 06:26 PM #2
Re: Pig Latin Translator
Your posted examples don't say what is wrong and how the output should look.
Write a small test program that uses split and prints out the results.Is there something wrong with my .split function
The java.util.Arrays toString() method is useful for displaying the contents of arrays.
- 09-10-2011, 06:51 PM #3
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
Re: Pig Latin Translator
Ok, so I did a test:
This is the output: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]); }
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?
- 09-10-2011, 06:54 PM #4
Re: Pig Latin Translator
What value are you using to end the for loop?ArrayIndexOutOfBoundsException: 4
Your variable names could be confusing you:
you use word for a sentence
and word2 a for an array of words
- 09-10-2011, 07:01 PM #5
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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.
- 09-10-2011, 07:04 PM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Pig Latin Translator
A hint. insert next code in your for{} loop
to see how long is your StringJava Code:System.out.println(word.length());
- 09-10-2011, 07:08 PM #7
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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 .splitLast edited by danthegreat; 09-10-2011 at 07:11 PM.
- 09-10-2011, 07:15 PM #8
Re: Pig Latin Translator
What value are you using to end the for loop?java.lang.ArrayIndexOutOfBoundsException: 4Last edited by Norm; 09-10-2011 at 07:27 PM.
- 09-10-2011, 07:16 PM #9
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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...
- 09-10-2011, 07:28 PM #10
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
Re: Pig Latin Translator
[I]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:
Shouldn't the bolded statements trigger when the word "Hi" is entered because it does not start with a vowel but has a vowel?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;
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.
- 09-10-2011, 07:32 PM #11
Re: Pig Latin Translator
Add some printlns to show the values of variables as they are set and changed to see where your logic is not working.Shouldn't the bolded statements trigger
- 09-10-2011, 07:42 PM #12
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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
I even tried to physically read off my code by hand: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]
Sharks
start="Sh"
end="arks"
fin = "arks" + "Sh" + "ay"
My code should work completely
- 09-10-2011, 07:44 PM #13
Re: Pig Latin Translator
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.There must be something wrong
- 09-10-2011, 08:02 PM #14
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Pig Latin Translator
Any progress?
what are your start and end variables when you print them?
- 09-10-2011, 08:04 PM #15
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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?
- 09-10-2011, 08:12 PM #16
Re: Pig Latin Translator
Look at line 68 and see how/why the index to substring is past the end of the StringStringIndexOutOfBoundsException: String index out of range: 7
at java.lang.String.substring(String.java:1934)
at IgpayAtinlay.wordToPig(IgpayAtinlay.java:68)
- 09-10-2011, 08:21 PM #17
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
- 09-10-2011, 08:24 PM #18
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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?
- 09-10-2011, 08:25 PM #19
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Pig Latin Translator
And just one question about your logic. Do you need to use
I think thatJava Code:substring(int start,int end)
is just enough for your needs...Java Code:substring(int start)
It seems like
is causing the problem...Java Code:english.length()-index)+1
Last edited by milovan; 09-10-2011 at 08:33 PM.
- 09-10-2011, 08:32 PM #20
Similar Threads
-
applicaton for nokia S60 devices(Translator), need help
By Basit781 in forum CLDC and MIDPReplies: 0Last Post: 05-18-2010, 03:42 PM -
java spanish translator needed for jni
By Nicholas Jordan in forum Jobs OfferedReplies: 5Last Post: 08-11-2009, 03:43 PM -
Custom Font Translator
By Nerdopolis in forum New To JavaReplies: 3Last Post: 04-18-2009, 03:50 AM -
Translator hashtable
By editor35 in forum New To JavaReplies: 1Last Post: 01-11-2009, 02:02 AM -
Java multilanguage translator
By mnprakash in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-05-2009, 08:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks