Advertisement
Guest User

Untitled

a guest
May 26th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PigLatin
  4. {
  5.  
  6. public static String translate(Scanner fileInput)
  7. throws java.io.FileNotFoundException, java.io.IOException
  8. {
  9. String latin = "";
  10. String word = "";
  11. String latinword = "";
  12. boolean firstword = true;
  13.  
  14. while ( fileInput.hasNext() )
  15. {
  16. word = fileInput.next();
  17.  
  18. latinword = "";
  19. boolean capital = false;
  20. int punc = 0;
  21. int puncBack = word.length();
  22. int prefixNum = 0;
  23. boolean puncTest = true;
  24. boolean begPunc = false;
  25. boolean allPunc = false;
  26. boolean digitWord = false;
  27. boolean firstVowel = false;
  28.  
  29. //--- Translate word ---
  30.  
  31. // Find whether capital or not
  32. boolean capitalTest = true;
  33. for ( int k = 0; k < word.length(); k++ )
  34. {
  35. if ( isLetter( word.charAt(k) ) && capitalTest )
  36. {
  37. if ( Character.isUpperCase( word.charAt(k) ) )
  38. {
  39. capital = true;
  40. // Make first letter lowercase
  41. word.substring(0,1).toLowerCase();
  42. }
  43. capitalTest = false;
  44. }
  45. }
  46.  
  47. // Find letters to move to back of word, if any
  48. boolean noVowel = false;
  49. for ( int k = 0; k < word.length(); k++ )
  50. {
  51. if ( isLetter( word.charAt(k) ) || isDigit( word.charAt(k) ) )
  52. {
  53. if ( isDigit( word.charAt(k) ) )
  54. {
  55. prefixNum = 1;
  56. puncTest = false;
  57. digitWord = true;
  58. break;
  59. }
  60. else if ( isLetter( word.charAt(k) ) )
  61. {
  62. if ( isVowel( word.charAt(k) ) )
  63. {
  64. prefixNum = k;
  65. puncTest = false;
  66. if (k == 0)
  67. firstVowel = true;
  68. break;
  69. }
  70. else if ( k == word.length() - 1 )
  71. {
  72. noVowel = true;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. if (puncTest)
  79. {
  80. punc = k;
  81. begPunc = true;
  82. }
  83. }
  84. }
  85. if ( punc < word.length() - 1 )
  86. {
  87. for ( int k = 1; k < word.length() - punc; k++ )
  88. {
  89. if ( !isLetter( word.charAt( word.length() - k ) ) && !isDigit( word.charAt( word.length() - k ) ) )
  90. {
  91. puncBack = word.length() - k;
  92. }
  93. }
  94. }
  95. else
  96. allPunc = true;
  97.  
  98. // Translate word
  99. if ( begPunc )
  100. {
  101. latinword = latinword + word.substring(0,punc+1);
  102. }
  103. latinword = latinword + word.substring(prefixNum,puncBack);
  104. if ( noVowel || firstVowel )
  105. latinword = word + "way";
  106. else
  107. {
  108. if ( begPunc )
  109. {
  110. if ( !allPunc )
  111. latinword = latinword + word.substring(punc+1,prefixNum);
  112. }
  113. else
  114. latinword = latinword + word.substring(punc,prefixNum);
  115.  
  116. latinword = latinword + "ay";
  117. }
  118. latinword = latinword + word.substring(puncBack,word.length());
  119.  
  120. if ( capital )
  121. latinword.substring(punc - 1,punc).toLowerCase();
  122. if ( digitWord )
  123. latinword = word + "way";
  124. if ( allPunc )
  125. latinword = word;
  126.  
  127. // Attach word to final string
  128. if ( firstword )
  129. latin = latinword;
  130. else
  131. latin = latin + " " + latinword;
  132.  
  133. firstword = false;
  134. }
  135.  
  136. return latin;
  137. }
  138.  
  139. public static boolean isLetter(char c)
  140. {
  141. final String letter = "qwertyuiopasdfghjklzxcvbnm";
  142. char test;
  143.  
  144. boolean letterTest = false;
  145.  
  146. for (int t=0; t < 26; t++)
  147. {
  148. test = letter.charAt(t);
  149.  
  150. if ( c == test )
  151. letterTest = true;
  152. }
  153.  
  154. return letterTest;
  155. }
  156.  
  157. public static boolean isDigit(char c)
  158. {
  159. final String digit = "0123456789";
  160. char test;
  161.  
  162. boolean digitTest = false;
  163.  
  164. for (int t=0; t < 10; t++)
  165. {
  166. test = digit.charAt(t);
  167.  
  168. if ( c == test )
  169. digitTest = true;
  170. }
  171.  
  172. return digitTest;
  173. }
  174.  
  175. public static boolean isVowel(char c)
  176. {
  177. final String vowel = "aeiouy";
  178. char test;
  179.  
  180. boolean vowelTest = false;
  181.  
  182. for (int t=0; t < 6; t++)
  183. {
  184. test = vowel.charAt(t);
  185.  
  186. if ( c == test )
  187. vowelTest = true;
  188. }
  189.  
  190. return vowelTest;
  191. }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement