I know I\'m close, but I need help with converting english to pig latin. The rul
ID: 3660499 • Letter: I
Question
I know I'm close, but I need help with converting english to pig latin. The rules are this: 1. if the word starts with a consonant, then move the first letter of the word to the end of the word and append "ay" to the new word. (ie. "ball" will become "allbay") 2. if the word starts with a vowel the first letter stays the same and "way" is appended to the end. (ie. "always" will become "alwaysway" Here is my code thus far: import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; public class PigLatin2 extends JFrame{ private JLabel prompt; private JTextField input; private JTextArea output; private int count; public PigLatin2(){ super("Pig Latin"); prompt = new JLabel("Enter a phrase to translate to Pig Latin:"); input = new JTextField(30); input.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String s = e.getActionCommand().toString(); StringTokenizer tokens = new StringTokenizer(s); count = tokens.countTokens(); while (tokens.hasMoreTokens()){ count--; printLatinWord(tokens.nextToken()); } } }); output = new JTextArea(10, 30); output.setEditable(false); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(prompt); c.add(input); c.add(output); setSize(500, 150); show(); } private void printLatinWord(String token){ char letters[] = token.toCharArray(); StringBuffer latinWord = new StringBuffer(); latinWord.append(Character.toLowerCase(letters[0])); if (letters[0] == 'a' || letters[0] == 'e' || letters[0] == 'i' || letters[0] == 'o' || letters[0] == 'u'){ latinWord.append(letters, letters.length -1, letters.length -1); latinWord.append("way"); }else{ latinWord.append(letters, 1, letters.length -1); latinWord.append("ay"); } output.append(latinWord.toString() + " "); if (count == 0) output.append(" "); } public static void main(String args[]){ PigLatin2 app = new PigLatin2(); app.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } }Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* PigGui: gives the Pig Latin translator a graphical interface.
*/
public class PigGui extends JFrame implements ActionListener {
// Data members: components of the GUI.
private JLabel pig;
private JLabel englishLabel;
private JTextArea englishText;
private JButton translate;
private JLabel latinLabel;
private JTextArea latinText;
/**
* Constructor: sets up the GUI.
*/
public PigGui() {
// Set some basic frame properties
this.setTitle("Pig Latin Translator");
this.setSize(500, 500);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get the content panel from inside the frame
Container panel = this.getContentPane();
// Not using a layout- doing absolute positioning
panel.setLayout(null);
// Construct the pig image, with border and tooltip
pig = new JLabel(new ImageIcon("pig.jpg"));
pig.setBorder(BorderFactory.createLineBorder(Color.black));
pig.setToolTipText("Oink");
// Construct the labels for english and latin text
englishLabel = new JLabel("English: ");
latinLabel = new JLabel("Pig Latin: ");
// Construct the area for text entry, with border and text wrap
englishText = new JTextArea();
englishText.setEditable(true);
englishText.setBorder(BorderFactory.createLoweredBevelBorder());
englishText.setLineWrap(true);
// Construct the area for text display, with border and text wrap
latinText = new JTextArea();
latinText.setEditable(false);
latinText.setBorder(BorderFactory.createRaisedBevelBorder());
latinText.setLineWrap(true);
// Construct the button for translation
translate = new JButton("Translate");
translate.addActionListener(this);
// Add all the components to the panel
panel.add(pig);
panel.add(englishLabel);
panel.add(englishText);
panel.add(translate);
panel.add(latinLabel);
panel.add(latinText);
// Set the locations of the components inside the panel
pig.setBounds(200,20,100,75);
englishLabel.setBounds(25,100,400,25);
englishText.setBounds(50,130,400,100);
translate.setBounds(200,260,100,40);
latinLabel.setBounds(25,320,100,25);
latinText.setBounds(50,350,400,100);
}
/**
* Main method: creates and displays translator.
*/
public static void main (String[] args) {
PigGui trans = new PigGui();
trans.setVisible(true);
}
/**
* Returns the string in Pig Latin.
* @param text The text to translate
* @return The translated version
*/
private String toPigLatin(String text) {
String translation = "";
// Go until there aren't any more words
while (!text.equals("")) {
// Get the first word
int wordEndIndex = findWordEnd(text);
String word = text.substring(0, wordEndIndex);
// Find the root and tail in pig latin
int vowelIndex = findVowel(word);
String root = word.substring(vowelIndex);
String tail = "-" + word.substring(0, vowelIndex) + "ay";
// Print the translated word
translation = translation + root + tail + " ";
// Cut first word off text (and spaces)
text = text.substring(wordEndIndex).trim();
}
return translation;
}
/**
* Finds the end of the first word in the string.
* @param text The text to look in
* @return The index of the first space
*/
private int findWordEnd(String text) {
int indexOfSpace = text.indexOf(" ");
if (indexOfSpace == -1) // Happens if there is no space
return text.length();
else
return indexofSpace;
}
/**
* Finds the first vowel in the word.
* @param word The word to look in
* @return The index of the first vowel
*/
private int findVowel(String word) {
// Look for a vowel, or the end of the word
for (int i=0; i if (word.charAt(i) == 'a' ||
word.charAt(i) == 'e' ||
word.charAt(i) == 'i' ||
word.charAt(i) == 'o' ||
word.charAt(i) == 'u' ) {
return i;
}
}
// Gets here if there aren't any vowels
return word.length();
}
/**
* Calls the translation method when there's a button click.
* @param event The ActionEvent caused by a button click
*/
public void actionPerformed(ActionEvent event) {
if (event.getSource() == translate) {
// Get the English they entered
String text = englishText.getText();
text = text.trim();
text = text.toLowerCase();
// Display the translation
latinText.setText(toPigLatin(text));
}
}
}