Advertisement
Guest User

Untitled

a guest
May 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 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 capital = false;
  13.  
  14. while ( fileInput.hasNext() )
  15. {
  16. word = fileInput.next();
  17.  
  18. int punc = 1;
  19. int prefixNum = 1;
  20. boolean puncTest = true;
  21. boolean prefixTest = true;
  22.  
  23. //--- Translate word ---
  24.  
  25. // Find whether capital or not
  26. boolean capitalTest = true;
  27. for ( int k = 0; k < word.length(); k++ )
  28. {
  29. if ( isLetter( word.charAt(k) ) && capitalTest )
  30. {
  31. if ( Character.isUpperCase( word.charAt(k) ) )
  32. {
  33. capital = true;
  34. // Make first letter lowercase
  35. word.substring(0,1).toLowerCase();
  36. }
  37. capitalTest = false;
  38. }
  39. }
  40.  
  41. // Find letters to move to back of word, if any
  42. for ( int k = 0; k < word.length() || prefixTest == false; k++ )
  43. {
  44. if ( isLetter( word.charAt(k) ) || isDigit( word.charAt(k) ) )
  45. {
  46. if ( isDigit( word.charAt(k) ) )
  47. {
  48. prefixNum = 1;
  49. prefixTest = false;
  50. }
  51. else if ( isLetter( word.charAt(k) ) )
  52. {
  53. if ( isVowel( word.charAt(k) ) )
  54. {
  55. prefixNum = k;
  56. prefixTest = false;
  57. }
  58. }
  59. }
  60. else
  61. {
  62. if (puncTest)
  63. {
  64. punc = k;
  65. puncTest = false;
  66. }
  67. }
  68. }
  69.  
  70. // Translate word
  71. if ( punc > 0 )
  72. {
  73. for ( int k = 0; k < punc; k++ )
  74. {
  75. latinword = latinword + word.substring(k,k+1);
  76. }
  77. }
  78. for ( int k = prefixNum; k < word.length(); k++ )
  79. {
  80. latinword = latinword + word.substring(k,k+1);
  81. }
  82. if ( prefixTest )
  83. latinword = latinword + "way";
  84. else
  85. {
  86. for ( int k = punc; k < prefixNum; k++ )
  87. {
  88. latinword = latinword + word.substring(k,k+1);
  89. }
  90. latinword = latinword + "ay";
  91. }
  92.  
  93. if ( capital )
  94. latinword.substring(punc - 1,punc).toLowerCase();
  95.  
  96. latin = latin + " " + latinword;
  97. }
  98.  
  99. return latin;
  100. }
  101.  
  102. public static boolean isLetter(char c)
  103. {
  104. final String letter = "qwertyuiopasdfghjklzxcvbnm";
  105. char test;
  106.  
  107. boolean letterTest = false;
  108.  
  109. for (int t=0; t < 26; t++)
  110. {
  111. test = letter.charAt(t);
  112.  
  113. if ( c == test )
  114. letterTest = true;
  115. }
  116.  
  117. return letterTest;
  118. }
  119.  
  120. public static boolean isDigit(char c)
  121. {
  122. final String digit = "0123456789";
  123. char test;
  124.  
  125. boolean digitTest = false;
  126.  
  127. for (int t=0; t < 10; t++)
  128. {
  129. test = digit.charAt(t);
  130.  
  131. if ( c == test )
  132. digitTest = true;
  133. }
  134.  
  135. return digitTest;
  136. }
  137.  
  138. public static boolean isVowel(char c)
  139. {
  140. final String vowel = "aeiouy";
  141. char test;
  142.  
  143. boolean vowelTest = false;
  144.  
  145. for (int t=0; t < 6; t++)
  146. {
  147. test = vowel.charAt(t);
  148.  
  149. if ( c == test )
  150. vowelTest = true;
  151. }
  152.  
  153. return vowelTest;
  154. }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement