Pig Latin Converter - Java Pt. 1

Chris Chen
/*
* Purpose: To convert an English sentence into Pig Latin.
*/
import java.util.*;
public class PigLatin {
public static void main(String[]args){
Scanner in = new Scanner(System.in);
String phrase;
System.out.print("English: ");
phrase = in.nextLine();
System.out.print("\nTranslation: " + phraseToPig(phrase));
}
public static boolean isCapitalized(String word){ //returns True if the word is capitalized
String firstLetter = word.substring(0,1), temp;
temp = firstLetter.toUpperCase();
return(firstLetter == temp);
}
public static String lowerCase(String word){ //converts String to Lower Case
return(word.toLowerCase());
}
public static String upperCase(String word){ //converts String to Upper Case
String firstLetter;
firstLetter = word.substring(0,1);
return(firstLetter.toUpperCase() + word.substring(1, word.length()));
}
public static String wordToPig(String english){ //converts english word to Pig Latin
String word = english;
int isCap = 0;
int position = hasAVowel(word);
if(isCapitalized(word)){
word = lowerCase(word);
isCap = 1;
}
if(position == word.length())
word += "ay";
else if(position == 0)
word += "yay";
else
word = word.substring(position, word.length()) + word.substring(0, position) + "ay";
if(isCap == 1)
word = upperCase(word);
return(word);
}
public static String phraseToPig(String phrase){ //converts english sentence to Pig Latin}
StringTokenizer token = new StringTokenizer(phrase);
String english, pigPhrase = "";
while(token.hasMoreTokens()){
english = token.nextToken();
pigPhrase += wordToPig(english) + " ";
}
return(pigPhrase);
}
public static int hasAVowel(String word){ //returns index of first vowel (all 10) (return -1 if no vowels in word)
int place = 0, lowest = word.length();
String vowel[] = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"};
for(int i = 0; i place = word.indexOf(vowel[i]);
if(place < lowest && place != -1)
lowest = place;
}
return(lowest);
}
}

Published by Chris Chen

Chris is currently attending the University of California, Berkeley seeking an undergraduate's degree in Electrical Engineering Computer Science. He enjoys playing basketball, practicing kendo, hanging out w...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.