Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By MaceMan

Thread: english to pig latin, I/O in gui

  1. #1
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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.

    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.");		
        }
    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!


    edit: I declared the array words elsewhere like this: public String[] words = new String[100];
    When in doubt, use a lightsaber

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  3. #3
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  5. #5
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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).
    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]);
    		}
        }
    }
    The output was as follows:
    how ay
    areway
    you ay
    doing ay

    Process completed.
    So it is properly filling my array, but for some reason, the gui isn't seeing it. Can you explain?
    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  7. #7
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: english to pig latin, I/O in gui

    Quote Originally Posted by MaceMan View Post
    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.
    Please post your code in the forum, not via private messaging. Likely it is an easy thing to fix.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  10. #10
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default Re: english to pig latin, I/O in gui

    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.");		
        	}
        }
    }
    Thanks alot man, I would really appreciate it if you would explain what I have done wrong, or incorrectly.

    (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

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: english to pig latin, I/O in gui

    Quote Originally Posted by MaceMan View Post
    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.");		
        	}
        }
    }
    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?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: english to pig latin, I/O in gui

    Another problem is here:

    Java Code:
          if (actionCommand.equals("Translate"))
             for (int i = 0; i < words.length; i++) {
                pigLatin.setText("Pig Latin: " + words[i]);
             }
    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.

  13. #13
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: english to pig latin, I/O in gui

    Quote Originally Posted by MaceMan View Post
    I'm thinking the best place to call it would be right inside my for loop(76)
    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)?

    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?
    It wants an array of String. Which is an array of String? words or words[3]?

  15. #15
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: english to pig latin, I/O in gui

    Quote Originally Posted by MaceMan View Post
    neither is a string, words[] is an array, which is why I don't understand how I am supposed to pass words[] to translateEnglish[].
    No, words[] is not an array. The array is String[] and the variable that represents this array is words. So...

    the array of Strings is words

    an individual String is represented by an item in the array, or for instance words[2]

  17. #17
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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

  18. #18
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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!

    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) :-)
    Fubarable likes this.
    When in doubt, use a lightsaber

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. English's role in a job as a programmer
    By Blue Solo in forum Jobs Discussion
    Replies: 1
    Last Post: 11-04-2011, 12:52 PM
  2. English to Binary Translator (Please help)
    By Baconator in forum New To Java
    Replies: 10
    Last Post: 10-01-2011, 10:15 PM
  3. Pig Latin Translator
    By danthegreat in forum New To Java
    Replies: 22
    Last Post: 09-10-2011, 11:42 PM
  4. Help with english..
    By ehochedez in forum Forum Lobby
    Replies: 3
    Last Post: 12-30-2009, 02:51 PM
  5. Be smart in English...
    By Eranga in forum Entertainment
    Replies: 22
    Last Post: 11-10-2009, 10:14 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
  •