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

Thread: Help with Pig Latin Translator...

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Pig Latin Translator...

    Hey guys, i am a newbie and having a problem getting text from a JTextArea to another JTextArea within the same Jframe using a button. The text in the Text Area West needs to be a user input/string, then when you press the button it will go to the translator and then output the string in Piglatin to the other TextArea West. I have the translator built but cannot figure out how to get the text from the text area using the scanner and then connect it to the button to have it append in the other text area. Any advice will help. The assigment is due Thursday and i am stressing out!


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Help with Pig Latin Translator...

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
     
     
    public class jchan extends JFrame{
    	private static JFrame window;
    	private static JTextArea jt1;
    	private static JTextArea jt2;
     
    	public jchan(){
     
    	}
     
    	public static void createAndShowGUI(){
    		window = new jchan();
     
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setLayout(new FlowLayout());
     
    		jt1 = new JTextArea("Hello");
    		jt2 = new JTextArea("Goodbye");
    		window.add(jt1);
    		window.add(jt2);
     
    		JButton button = new JButton("convert");
    		button.addActionListener(new ActionListener(){
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				jt2.setText(jt1.getText());
     
    			}
     
    		});
    		window.add(button);
    		window.pack();
    		window.setSize(200, 200);
    		window.setVisible(true);
     
    	}
     
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
    	}
     
    }
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Pig Latin Translator...

    Thank you so much, you are a lifesaver! I have been trying to figure that one out for a few days. It all makes sense now. Its always hard when your just learning a new language. Now i just need to try and tie in the translator class to the button. Thanks again!

  4. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Pig Latin Translator...

    I am still confused as to how i can call the method from the Translator and run it through the button. Here is the translator code and the code for the frame with the main method. Any advice will greatly enhance my learning experience sense i have spent hours reading the Java documentations. BTW, this is only my second homework assignment!
    // Here is the Translator

    import java.util.Scanner;

    public class Translator
    {
    //public static void main(String[] args)
    public String translate()
    {

    Scanner sc = new Scanner(System.in);
    while(sc.hasNextLine())
    {
    String line = sc.nextLine().trim().toLowerCase();
    while(line.length() > 0) {
    int blank = line.indexOf(' ');
    String word = "";

    if(blank > 0) {
    word = line.substring(0, blank);
    line = line.substring(blank).trim();
    } else {
    word = line;
    line = "";
    }

    int a = word.indexOf('a');
    int e = word.indexOf('e');
    int i = word.indexOf('i');
    int o = word.indexOf('o');
    int u = word.indexOf('u');

    if((a == -1)&&(e == -1) && (i == -1) &&
    (o == -1) && (u == -1))
    {
    word = word + "ay";
    } else {
    if((a == 0)||(e == 0)||(i == 0) || (o == 0) ||
    (u == 0))
    {
    word = word + "way";
    } else {
    int n = word.length();
    if((a >= 0) && (a < n))
    n = a;
    if((e >= 0) && (e < n))
    n = e;
    if((i >= 0) && (i < n))
    n = i;
    if((o >= 0) && (o < n))
    n = o;
    if((u >= 0) && (u < n))
    n = u;
    word = word.substring(n) +
    word.substring(0, n) + "ay";
    }
    }
    System.out.print(word + " ");
    }
    //System.out.println();
    }
    }
    }




    // Here is the frame class with the button and main

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.*;

    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;



    /**
    This is a frame class for the translator

    9.24.09
    */

    public class jchan extends JFrame{
    private static JFrame window;
    private static JTextArea jt1;
    private static JTextArea jt2;
    private static JScrollPane areaScrollPaneE;
    private static JScrollPane areaScrollPaneW;

    public jchan(){

    }

    public static void createAndShowGUI(){
    window = new jchan();
    window.setTitle("Pig Latin Translator");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    window.setLayout(new FlowLayout());
    window.setLayout(new BorderLayout());


    // Creates the JTextAreas

    jt1 = new JTextArea("Enter text here");
    jt2 = new JTextArea();
    window.add(jt1);
    window.add(jt2);


    // Allows for scrolling of the text area west

    JScrollPane areaScrollPaneW = new JScrollPane(jt1);
    areaScrollPaneW.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    areaScrollPaneW.setPreferredSize(new Dimension(300,100));
    window.add(areaScrollPaneW, BorderLayout.WEST);


    // Allows for scrolling of the text area east

    JScrollPane areaScrollPaneE = new JScrollPane(jt2);
    areaScrollPaneE.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    areaScrollPaneE.setPreferredSize(new Dimension(300,100));
    window.add(areaScrollPaneE, BorderLayout.EAST);

    // Button Panel South

    JPanel buttonPanel = new JPanel( );
    buttonPanel.setBackground(Color.BLACK);
    buttonPanel.setLayout(new FlowLayout( ));


    JButton button = new JButton("Translate");
    buttonPanel.add(button);
    window.add(buttonPanel, BorderLayout.SOUTH);
    button.addActionListener(new ActionListener(){

    // THIS IS WHERE IAM HAVING THE PROBLEM CALLING THE METHOD
    @Override
    public void actionPerformed(ActionEvent arg0) {
    jt2.setText(translate(jt1.getText()));

    }

    });
    //window.add(button);
    window.pack();
    window.setSize(700, 250);
    window.setVisible(true);

    }

    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }

    }

    Thanks.....

Similar Threads

  1. Pig Latin Translator
    By BMN901 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 17th, 2009, 03:31 AM