Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PigLatin
- {
- public static String translate(Scanner fileInput)
- throws java.io.FileNotFoundException, java.io.IOException
- {
- String latin = "";
- String word = "";
- String latinword = "";
- boolean firstword = true;
- while ( fileInput.hasNext() )
- {
- word = fileInput.next();
- latinword = "";
- boolean capital = false;
- int punc = 0;
- int puncBack = word.length();
- int prefixNum = 0;
- boolean puncTest = true;
- boolean begPunc = false;
- boolean allPunc = false;
- boolean digitWord = false;
- boolean firstVowel = false;
- //--- Translate word ---
- // Find whether capital or not
- boolean capitalTest = true;
- for ( int k = 0; k < word.length(); k++ )
- {
- if ( isLetter( word.charAt(k) ) && capitalTest )
- {
- if ( Character.isUpperCase( word.charAt(k) ) )
- {
- capital = true;
- // Make first letter lowercase
- word.substring(0,1).toLowerCase();
- }
- capitalTest = false;
- }
- }
- // Find letters to move to back of word, if any
- boolean noVowel = false;
- for ( int k = 0; k < word.length(); k++ )
- {
- if ( isLetter( word.charAt(k) ) || isDigit( word.charAt(k) ) )
- {
- if ( isDigit( word.charAt(k) ) )
- {
- prefixNum = 1;
- puncTest = false;
- digitWord = true;
- break;
- }
- else if ( isLetter( word.charAt(k) ) )
- {
- if ( isVowel( word.charAt(k) ) )
- {
- prefixNum = k;
- puncTest = false;
- if (k == 0)
- firstVowel = true;
- break;
- }
- else if ( k == word.length() - 1 )
- {
- noVowel = true;
- }
- }
- }
- else
- {
- if (puncTest)
- {
- punc = k;
- begPunc = true;
- }
- }
- }
- if ( punc < word.length() - 1 )
- {
- for ( int k = 1; k < word.length() - punc; k++ )
- {
- if ( !isLetter( word.charAt( word.length() - k ) ) && !isDigit( word.charAt( word.length() - k ) ) )
- {
- puncBack = word.length() - k;
- }
- }
- }
- else
- allPunc = true;
- // Translate word
- if ( begPunc )
- {
- latinword = latinword + word.substring(0,punc+1);
- }
- latinword = latinword + word.substring(prefixNum,puncBack);
- if ( noVowel || firstVowel )
- latinword = word + "way";
- else
- {
- if ( begPunc )
- {
- if ( !allPunc )
- latinword = latinword + word.substring(punc+1,prefixNum);
- }
- else
- latinword = latinword + word.substring(punc,prefixNum);
- latinword = latinword + "ay";
- }
- latinword = latinword + word.substring(puncBack,word.length());
- if ( capital )
- latinword.substring(punc - 1,punc).toLowerCase();
- if ( digitWord )
- latinword = word + "way";
- if ( allPunc )
- latinword = word;
- // Attach word to final string
- if ( firstword )
- latin = latinword;
- else
- latin = latin + " " + latinword;
- firstword = false;
- }
- return latin;
- }
- public static boolean isLetter(char c)
- {
- final String letter = "qwertyuiopasdfghjklzxcvbnm";
- char test;
- boolean letterTest = false;
- for (int t=0; t < 26; t++)
- {
- test = letter.charAt(t);
- if ( c == test )
- letterTest = true;
- }
- return letterTest;
- }
- public static boolean isDigit(char c)
- {
- final String digit = "0123456789";
- char test;
- boolean digitTest = false;
- for (int t=0; t < 10; t++)
- {
- test = digit.charAt(t);
- if ( c == test )
- digitTest = true;
- }
- return digitTest;
- }
- public static boolean isVowel(char c)
- {
- final String vowel = "aeiouy";
- char test;
- boolean vowelTest = false;
- for (int t=0; t < 6; t++)
- {
- test = vowel.charAt(t);
- if ( c == test )
- vowelTest = true;
- }
- return vowelTest;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement