Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- //This program translates a sentence in english to pig latin
- //BY: Seby Stephens
- public class Pig_Latin {
- public static void main(String[] args){
- //Stores the sentence user inputs into a variable
- String userSentence = getInfo();
- String translatedSentence = "";
- //Stores the splitStr return value into an array
- //Makes for loop on next line easier
- String[] array = splitStr(userSentence);
- //For each value of the array this code is run
- for(int i = 0; i < array.length; i++){
- //Gets the word from the first array value
- String wordFromSentence = array[i];
- //Translates it and stores into variable
- translatedSentence += translateInfo(wordFromSentence) + " ";
- }
- //Prints out translated sentence
- System.out.println(translatedSentence);
- }
- /**
- * Gets sentence from the user in english
- * @param - n/a
- * @return - the sentence the user inputs
- */
- static String getInfo(){
- //Lets user input information using scanner
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter sentence you want translated to pig latin: ");
- String text = scan.nextLine();
- return text;
- }
- /**
- * Splits the sentence and stores it into array
- * @param userSentence
- * @return - the array with each word as a value
- */
- static String[] splitStr(String userSentence){
- String[] arrOfStr = userSentence.split(" ", 200);
- return arrOfStr;
- }
- /**
- * Checks to see if character is a vowel
- * @param c - character from each word in the sentence user inputs
- * @return - boolean
- */
- static boolean isVowel(char c){
- if( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ) {
- return true;
- }
- return false;
- }
- /**
- * Translates a single word into pig latin
- * @param wordFromSentence
- * @return - string
- */
- static String translateInfo(String wordFromSentence){
- //Sets count variable
- int count = -1;
- int checkVowelInLetterOne = 0;
- //For each letter in the word from the sentence the user entered
- for (int i = 0; i < wordFromSentence.length(); i++) {
- //Looks for the first vowel in the word
- if (isVowel(wordFromSentence.charAt(i))) {
- //Sets count equal to that value and breaks the loop
- count = i;
- //Changes checkVowelInLetterOne to 2 if there is a vowel there
- if(isVowel(wordFromSentence.charAt(0))){
- checkVowelInLetterOne = 2;
- }
- break;
- }
- }
- //The word has no vowels
- //Treats y as a vowel
- if (count == -1) {
- int counterTwo = 0;
- for (int i = 0; i < wordFromSentence.length(); i++) {
- if (wordFromSentence.charAt(i) == 'y'){
- counterTwo = i;
- break;
- }
- }
- return wordFromSentence.substring(counterTwo)+ wordFromSentence.substring(0,counterTwo) + "ay";
- }
- else {
- //If vowel is first letter then add yay instead of ay to the end
- if(checkVowelInLetterOne == 2){
- return wordFromSentence.substring(count) + wordFromSentence.substring(0, count) + "yay";
- }
- else {
- //Returns a string made up of multiple substrings and "ay" at the end
- //First substring has the word after the value of count; so it starts with vowel
- //Second has from the first character to count: these are the consonants that are moved back
- //"ay" is needed at the end of every word
- return wordFromSentence.substring(count) + wordFromSentence.substring(0, count) + "ay";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement