0

For my code I need to be able to do a pig latin translation for a word but I need help in doing a full phrase including punctuation such as periods

The code I have so far only does one individual word but Im not sure how to include a full phrase

Example

public PigLatinTranslator ()
{
    super ("Pig Latin Translator");   // Set the frame's name
    frame = getContentPane ();
    lblInput = new Label ("Enter a word in English");
    lblOutput = new Label ("Translation to Pig Latin");
    txtInput = new TextField (50);
    txtOutput = new TextField (50);

    btnTranslate = new Button ("Translate");
    btnClear = new Button ("Clear");
    btnCancel = new Button ("Cancel");

    frame.setLayout (new GridLayout (4, 2));

    btnTranslate.addActionListener (this);
    btnClear.addActionListener (this);
    btnCancel.addActionListener (this);

    frame.add (lblInput);
    frame.add (txtInput);
    frame.add (lblOutput);
    frame.add (txtOutput);
    frame.add (btnTranslate);
    frame.add (btnClear);
    frame.add (btnCancel);

    setSize (400, 200);     // Set the frame's size
    setVisible (true);                // Show the frame
    setLocation (100, 100);
    setDefaultCloseOperation (EXIT_ON_CLOSE);
} // Constructor


public static void main (String[] args)
{
    new PigLatinTranslator ();      // Create a PigLatinTranslator frame
} // main method


public void actionPerformed (ActionEvent event)
{
    if (event.getSource () == btnClear)
    {
        txtInput.setText ("");
        txtOutput.setText ("");
    }


    if (event.getSource () == btnTranslate)
    {
        int[] anArray;
        String word = txtInput.getText ();

        String[] parts = word.split ("\\s+");
        for (if           
        String newWord = "";
        if (word.length () <= 1)
        {
            newWord = word;
        }
        else
        {
            char word2;
            word2 = word.charAt (0);
            if (QUChecker.startsWithQU (word) == true)
            {
                newWord = word.substring (2);
                newWord = newWord + "quay";
            }

            else if (PigMethods.checkAscii (word2) == true)
            {
                newWord = word + "way";
            }
            else
            {
                String letter = "";
                letter = word.substring (0, 1);
                newWord = word.substring (1);
                newWord = newWord + letter + "ay";
            }
        }

        txtOutput.setText (newWord);

    }

    else if (event.getSource () == btnCancel)
    {
        System.exit (0);
    }
reo katoa
  • 5,751
  • 1
  • 18
  • 30
  • 1
    For future reference, when you post a question include the minimal code necessary to show the problem. You can remove all the GUI code and just show us the pig latin logic. See http://stackoverflow.com/help/mcve – Duncan Jones May 09 '14 at 14:31
  • 1
    Also `for (if String newWord = "";` is not valid code. Please give us something that compiles. – Duncan Jones May 09 '14 at 14:32

1 Answers1

0

If you have logic that works for one word, you can make use of String.split() to create an array of Strings, which you can then just iterate over and append to recreate the entire phrase.

See: Split string into individual words Java

Community
  • 1
  • 1
Matt R.
  • 51
  • 5