Monday, March 16, 2015

Pig Latin Translator v1.0

Finally got the pig latin translator working properly (mostly). It accounts for special cases like qu, ph, ch, and sh. However, I have not yet implemented the ability to search for vowels past the first two characters. This requires a lot of extra booleans and reformatting my entire code. Here's the current code, nonetheless:

import javax.swing.JOptionPane;

public class NoPleaseStop {

 public static void main(String[] args){
  String user_input = JOptionPane.showInputDialog("Enter a word to 
translate:").toLowerCase();
  String[] vowels = {"a", "e", "i", "o", "u"}; //array of vowels
  String result = ""; //creates empty string for final translation
  boolean check = false; //Used for vowel/consonant rules
  
        if (isAlpha(user_input) == false) { //A little code Brian 
showed me, checks for proper notation
            JOptionPane.showMessageDialog(null, "No extra symbols 
or sentences, please. Just one word.", "USER ERROR", JOptionPane
.INFORMATION_MESSAGE); //Explains user error
        } else if (user_input.startsWith("qu")) { //Here's some 
extra rules
         result = user_input.substring(2) + "quay";
         JOptionPane.showMessageDialog(null, result, "Translation",
 JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else if (user_input.startsWith("ph")) {
         result = user_input.substring(2) + "phay";
         JOptionPane.showMessageDialog(null, result, "Translation"
JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else if (user_input.startsWith("sh")) {
         result = user_input.substring(2) + "shay";
         JOptionPane.showMessageDialog(null, result, "Translation"
JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else if (user_input.startsWith("ch")) {
         result = user_input.substring(2) + "chay";
         JOptionPane.showMessageDialog(null, result, "Translation"
JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else {
          for (int i = 0; i < vowels.length; i++) {
           if (user_input.startsWith(vowels[i])) {
            check = true;
           }
          }
          if (check) {
           result = user_input + "yay"; //vowel rule
          } else {
           result = user_input.substring(1) + user_input.charAt(0)
 + "ay"; //consonant rule
          }
          JOptionPane.showMessageDialog(null, result, "Translation",
 JOptionPane.INFORMATION_MESSAGE); //Prints the translation
      }
 }
 
    public static boolean isAlpha(String user_input) {  
//The other bit of the code Brian showed me
        char[] chars = user_input.toCharArray(); 
//It checks for correct characters!
        for (char c : chars) {
            if(!Character.isLetter(c)) {
                return false;
            }
        }
        return true; 
    }
    
}
 
Sorry for the poor formatting, Blogger doesn't like code. Here's some example inputs/outputs:







No comments:

Post a Comment