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 capital = false;
- while ( fileInput.hasNext() )
- {
- word = fileInput.next();
- int punc = 1;
- int prefixNum = 1;
- boolean puncTest = true;
- boolean prefixTest = true;
- //--- 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
- for ( int k = 0; k < word.length() || prefixTest == false; k++ )
- {
- if ( isLetter( word.charAt(k) ) || isDigit( word.charAt(k) ) )
- {
- if ( isDigit( word.charAt(k) ) )
- {
- prefixNum = 1;
- prefixTest = false;
- }
- else if ( isLetter( word.charAt(k) ) )
- {
- if ( isVowel( word.charAt(k) ) )
- {
- prefixNum = k;
- prefixTest = false;
- }
- }
- }
- else
- {
- if (puncTest)
- {
- punc = k;
- puncTest = false;
- }
- }
- }
- // Translate word
- if ( punc > 0 )
- {
- for ( int k = 0; k < punc; k++ )
- {
- latinword = latinword + word.substring(k,k+1);
- }
- }
- for ( int k = prefixNum; k < word.length(); k++ )
- {
- latinword = latinword + word.substring(k,k+1);
- }
- if ( prefixTest )
- latinword = latinword + "way";
- else
- {
- for ( int k = punc; k < prefixNum; k++ )
- {
- latinword = latinword + word.substring(k,k+1);
- }
- latinword = latinword + "ay";
- }
- if ( capital )
- latinword.substring(punc - 1,punc).toLowerCase();
- latin = latin + " " + latinword;
- }
- 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