Results 1 to 19 of 19
Thread: english to pig latin, I/O in gui
- 11-12-2011, 05:12 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
english to pig latin, I/O in gui
I'm trying to write a program that takes an input in standered english, and then outputs it in pig latin, using a gui with two JTextArea's to input and output the sentence. I have succesfully made my gui, but I have a runtime error that says:
Pig Latin: [Ljava.lang.String;@fa9cf
Here is my code, before I say anything else. I'm pretty sure the problem is somewhere within these two methods, and not where I create my gui.
I think the problem is in line 26, where I output the array 'words'. I'm not sure if I'm outputing it correctly, or if I should really store my words in an array to begin with. There might also be some incorrect coding in the method translateEnglish(). I keep going back and forth between char and String, so if you guys see anything wrong, thanks in advance for your help!Java Code:public void translateEnglish() { String sentence = english.getText(); words = sentence.split(" "); for (String word : words) { Character firstLetter = word.charAt(0); if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter =='u') { word = word + "way"; } else //We don't need to give this a argument, becuase if it's not a vowel, it must be a consonant. { firstLetter = ' '; word = word + firstLetter + "ay"; } } } public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("Translate")) pigLatin.setText("Pig Latin: " + words); else if (!(actionCommand.equals("Translate"))) pigLatin.setText("Error."); }.gif)
edit: I declared the array words elsewhere like this: public String[] words = new String[100];When in doubt, use a lightsaber
-
Re: english to pig latin, I/O in gui
You're not getting a "run time error", but rather the default toString() representation of an array of String, which is what words is. You may wish to show a specific String from the array in which case you'll need to do something like ("Pig Latin: " + words[index]) -- the index will need to correspond to whichever String from words that you want to display.
- 11-12-2011, 05:29 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Re: english to pig latin, I/O in gui
Thanks, I put in a for loop to iterate through my array and output it, which took care of that problem, but now I am getting:
Pig Latin: null
So either I have nothing in my array, or I'm doing nothing to it. Any ideas why?When in doubt, use a lightsaber
-
Re: english to pig latin, I/O in gui
I wonder if your array is filled with Strings yet as my guess is that it holds null values at the present time.
- 11-12-2011, 06:57 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Re: english to pig latin, I/O in gui
I am pretty sure my array is being filled, because I made a small test program to see if it was being filled or not, (with a few tweaks to make it work by itself).
The output was as follows:Java Code:public class testPE { public static void main(String[] args) { String[] words = new String[100]; String sentence = "how are you doing"; words = sentence.split(" "); for (int i = 0; i < words.length; i++) { Character firstLetter = words[i].charAt(0); if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter =='u') { words[i] = words[i] + "way"; } else //We don't need to give this a argument, becuase if it's not a vowel, it must be a consonant. { firstLetter = ' '; words[i] = words[i] + firstLetter + "ay"; } } for (int i = 0; i < words.length; i++) { System.out.println(words[i]); } } }
So it is properly filling my array, but for some reason, the gui isn't seeing it. Can you explain?how ay
areway
you ay
doing ay
Process completed.
Also, as you can see, I obviously havn't coded the part where it translates into pig latin.
Thanks for the help
When in doubt, use a lightsaber
-
Re: english to pig latin, I/O in gui
I can't tell without a runnable program that I could test and modify.
- 11-12-2011, 07:10 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Re: english to pig latin, I/O in gui
let me pm you my program, it's only a 100 lines, so it's not that big. If you could take a look at it, I would be extremely thankful.
When in doubt, use a lightsaber
-
Re: english to pig latin, I/O in gui
-
Re: english to pig latin, I/O in gui
And in fact, I see some things that are wrong, and will happily explain it to you, but to do so, I will have to post your program here. It's up to you.
- 11-12-2011, 07:39 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Re: english to pig latin, I/O in gui
Thanks alot man, I would really appreciate it if you would explain what I have done wrong, or incorrectly.Java Code:public class PigTranslator extends JFrame implements ActionListener { public static final int WIDTH = 600; public static final int HEIGHT = 325; public static final int NUMBER_OF_ENGLISH_CHARS = 50; public static final int NUMBER_OF_PIG_CHARS = 50; public String[] words = new String[100]; private JTextArea english; private JTextArea pigLatin; public static void main(String[] args) { PigTranslator gui = new PigTranslator(); gui.setVisible(true); } public PigTranslator() { super("PigLatin Translator"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); JPanel contentPane = new JPanel(); contentPane.setLayout(new FlowLayout()); english = new JTextArea("Enter source text here.", 3, NUMBER_OF_ENGLISH_CHARS); english.setBackground(Color.WHITE); contentPane.add(english); add(contentPane, BorderLayout.NORTH); pigLatin = new JTextArea(3, NUMBER_OF_PIG_CHARS); pigLatin.setBackground(Color.WHITE); contentPane.add(pigLatin); add(contentPane, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.GREEN); buttonPanel.setLayout(new FlowLayout()); JButton translateButton = new JButton("Translate"); translateButton.addActionListener(this); buttonPanel.add(translateButton); add(buttonPanel, BorderLayout.SOUTH); } public void translateEnglish() { String sentence = english.getText(); words = sentence.split(" "); for (int i = 0; i < words.length; i++) { Character firstLetter = words[i].charAt(0); if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter =='u') { words[i] = words[i] + "way"; } else //We don't need to give this a argument, becuase if it's not a vowel, it must be a consonant. { firstLetter = ' '; words[i] = words[i] + firstLetter + "ay"; } } } public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("Translate")) for (int i = 0; i < words.length; i++) { pigLatin.setText("Pig Latin: " + words[i]); } else if (!(actionCommand.equals("Translate"))) { pigLatin.setText("Error."); } } }
(I'm still excited that I actually made a JFrame, no matter how messed up it is, lol)Last edited by MaceMan; 11-12-2011 at 08:02 PM.
When in doubt, use a lightsaber
-
Re: english to pig latin, I/O in gui
Thanks.
Now, to see if you are filling the words array, you need to find the method that does this -- translateEnglish().
Next you have to see if you call this method before trying to use the words array, since if you don't, then words will in fact be null. If not, where would be the best place to call this method?
-
Re: english to pig latin, I/O in gui
Another problem is here:
This will go loop through the words array. Each time it loops, it removes the previous String in the pigLatin JTextField and replaces it with the next String. It will do this almost instantaneous, and thus all you will see is the last word in the array. Perhaps instead you want to display all the words in this field by first creating a new String (or StringBuilder if you want to get fancy), adding all the Strings found in words, and then displaying this String.Java Code:if (actionCommand.equals("Translate")) for (int i = 0; i < words.length; i++) { pigLatin.setText("Pig Latin: " + words[i]); }
- 11-12-2011, 08:15 PM #13
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Re: english to pig latin, I/O in gui
I'm thinking the best place to call it would be right inside my for loop(76)
So I inserted this piece of code: translateEnglish(words[i]);
but I'm getting the error: translateEnglish() in PigTranslator cannot be applied to (java.lang.String)
So if I can't apply it to a string, does it want a Char?When in doubt, use a lightsaber
-
Re: english to pig latin, I/O in gui
Hm,... does it make more sense to construct an array once before you loop through it and extract what it contains, or to continually re-construct it inside of the loop (meaning to reconstruct the same array over and over again each time the for loop loops)?
It wants an array of String. Which is an array of String? words or words[3]?So I inserted this piece of code: translateEnglish(words[i]);
but I'm getting the error: translateEnglish() in PigTranslator cannot be applied to (java.lang.String)
So if I can't apply it to a string, does it want a Char?
- 11-12-2011, 09:05 PM #15
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Re: english to pig latin, I/O in gui
neither is a string, words[] is an array, which is why I don't understand how I am supposed to pass words[] to translateEnglish[].
I have no clue why my program won't output words[]. Here is my most current attempt, I implemented what you said, about making a string of the words I wanted to output, but now I only get a single null.
Java Code:public String translateEnglish() { String sentence = english.getText(); words = sentence.split(" "); for (int i = 0; i < words.length; i++) { Character firstLetter = words[i].charAt(0); if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter =='u') { words[i] = words[i] + "way"; wordsToOutput = wordsToOutput + " " + words[i]; } else //We don't need to give this a argument, becuase if it's not a vowel, it must be a consonant. { firstLetter = ' '; words[i] = words[i] + firstLetter + "ay"; wordsToOutput = wordsToOutput+ " " + words[i]; } } return wordsToOutput; } public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("Translate")) { pigLatin.setText("Pig Latin: " + wordsToOutput); } else if (!(actionCommand.equals("Translate"))) { pigLatin.setText("Error."); } } }When in doubt, use a lightsaber
-
Re: english to pig latin, I/O in gui
- 11-12-2011, 10:02 PM #17
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Re: english to pig latin, I/O in gui
I think I understand that now... but I am still getting the error:
translateEnglish() in PigTranslator cannot be applied to (java.lang.String)
when I try invoke translateEnglish, for example: translateEnglish(words[2]);When in doubt, use a lightsaber
- 11-13-2011, 01:34 AM #18
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Re: english to pig latin, I/O in gui
Well, I finished my gui problem, and I just wanted to thank you for your help. You didn't tell me the answer, you helped me reach it, and I will remember all I learned today becuase of that. Thanks a ton and 3/4 man! you rock!
.gif)
P.S (I redid translateEnglish, so I could pass a String to it, and then redid the work done on the words, and everything finally fell together) :-)When in doubt, use a lightsaber
-
Re: english to pig latin, I/O in gui
Glad that you've succeeded and did it on your own -- good show!
Similar Threads
-
English's role in a job as a programmer
By Blue Solo in forum Jobs DiscussionReplies: 1Last Post: 11-04-2011, 12:52 PM -
English to Binary Translator (Please help)
By Baconator in forum New To JavaReplies: 10Last Post: 10-01-2011, 10:15 PM -
Pig Latin Translator
By danthegreat in forum New To JavaReplies: 22Last Post: 09-10-2011, 11:42 PM -
Help with english..
By ehochedez in forum Forum LobbyReplies: 3Last Post: 12-30-2009, 02:51 PM -
Be smart in English...
By Eranga in forum EntertainmentReplies: 22Last Post: 11-10-2009, 10:14 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks