Advertisement
seby_stephens

Pig Latin Translator

Mar 5th, 2020
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. import java.util.Scanner;
  2. //This program translates a sentence in english to pig latin
  3. //BY: Seby Stephens
  4. public class Pig_Latin {
  5.     public static void main(String[] args){
  6.         //Stores the sentence user inputs into a variable
  7.         String userSentence  = getInfo();
  8.         String translatedSentence = "";
  9.         //Stores the splitStr return value into an array
  10.         //Makes for loop on next line easier
  11.         String[] array = splitStr(userSentence);
  12.         //For each value of the array this code is run
  13.         for(int i = 0; i < array.length; i++){
  14.             //Gets the word from the first array value
  15.             String wordFromSentence = array[i];
  16.             //Translates it and stores into variable
  17.             translatedSentence += translateInfo(wordFromSentence) + " ";
  18.         }
  19.         //Prints out translated sentence
  20.         System.out.println(translatedSentence);
  21.     }
  22.  
  23.     /**
  24.      * Gets sentence from the user in english
  25.      * @param - n/a
  26.      * @return - the sentence the user inputs
  27.      */
  28.     static String getInfo(){
  29.         //Lets user input information using scanner
  30.         Scanner scan = new Scanner(System.in);
  31.         System.out.println("Enter sentence you want translated to pig latin: ");
  32.         String text = scan.nextLine();
  33.         return text;
  34.     }
  35.  
  36.     /**
  37.      * Splits the sentence and stores it into array
  38.      * @param userSentence
  39.      * @return - the array with each word as a value
  40.      */
  41.     static String[] splitStr(String userSentence){
  42.         String[] arrOfStr = userSentence.split(" ", 200);
  43.         return arrOfStr;
  44.     }
  45.  
  46.     /**
  47.      * Checks to see if character is a vowel
  48.      * @param c - character from each word in the sentence user inputs
  49.      * @return - boolean
  50.      */
  51.     static boolean isVowel(char c){
  52.         if( c ==  'a' || c ==  'e' || c ==  'i' || c ==  'o' || c ==  'u' || c ==  'A' || c ==  'E' || c ==  'I' || c ==  'O' || c ==  'U'  ) {
  53.             return true;
  54.         }
  55.             return false;
  56.     }
  57.  
  58.     /**
  59.      * Translates a single word into pig latin
  60.      * @param wordFromSentence
  61.      * @return - string
  62.      */
  63.     static String translateInfo(String wordFromSentence){
  64.         //Sets count variable
  65.         int count = -1;
  66.         int checkVowelInLetterOne = 0;
  67.         //For each letter in the word from the sentence the user entered
  68.         for (int i = 0; i < wordFromSentence.length(); i++) {
  69.             //Looks for the first vowel in the word
  70.             if (isVowel(wordFromSentence.charAt(i))) {
  71.                 //Sets count equal to that value and breaks the loop
  72.                 count = i;
  73.                 //Changes checkVowelInLetterOne to 2 if there is a vowel there
  74.                 if(isVowel(wordFromSentence.charAt(0))){
  75.                     checkVowelInLetterOne = 2;
  76.                 }
  77.                 break;
  78.             }
  79.         }
  80.         //The word has no vowels
  81.         //Treats y as a vowel
  82.         if (count == -1) {
  83.             int counterTwo = 0;
  84.             for (int i = 0; i < wordFromSentence.length(); i++) {
  85.                 if (wordFromSentence.charAt(i) == 'y'){
  86.                     counterTwo = i;
  87.                     break;
  88.                 }
  89.             }
  90.             return wordFromSentence.substring(counterTwo)+ wordFromSentence.substring(0,counterTwo) + "ay";
  91.         }
  92.         else {
  93.             //If vowel is first letter then add yay instead of ay to the end
  94.             if(checkVowelInLetterOne == 2){
  95.                 return wordFromSentence.substring(count) + wordFromSentence.substring(0, count) + "yay";
  96.             }
  97.             else {
  98.                 //Returns a string made up of multiple substrings and "ay" at the end
  99.                 //First substring has the word after the value of count; so it starts with vowel
  100.                 //Second has from the first character to count: these are the consonants that are moved back
  101.                 //"ay" is needed at the end of every word
  102.                 return wordFromSentence.substring(count) + wordFromSentence.substring(0, count) + "ay";
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement