Guest User

Untitled

a guest
May 24th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. // Name: Anthony Cosentini
  2. // Course: CIS1068 Section 1
  3. // Date: 10/13/2008
  4. // Homework submission for homework 6.
  5.  
  6. import java.util.Scanner;
  7. public class Lab6 {
  8. public static String getNysiis(String s) {
  9. s = s.toUpperCase();
  10. String t = "";
  11. // If the last character is an 'A' or an 'S' remove it...
  12. if (s.charAt(s.length() - 1) == 'A' || s.charAt(s.length() -1) == 'S')
  13. {
  14. s = s.substring(0, s.length()-1);
  15. }
  16.  
  17. // Do the first part of the name...
  18. int firstPartLength = 0;
  19. if (s.substring(0,3).equals("MAC")) {
  20. t += "MCC";
  21. firstPartLength = 3;
  22. } else if (s.substring(0,2).equals("KN")) {
  23. t += "NN";
  24. firstPartLength = 2;
  25. } else if (s.substring(0,1).equals("K")) {
  26. t += "C";
  27. firstPartLength = 1;
  28. } else if (s.substring(0,2).equals("PH")) {
  29. t += "FF";
  30. firstPartLength = 2;
  31. }
  32.  
  33. // Figure out the end of the word...
  34. String endOfWord = s.substring(s.length()-2, s.length());
  35. boolean trigger = true;
  36. boolean secondTrigger = false;
  37. boolean useEnd = false;
  38. char end = 'D';
  39.  
  40. if (endOfWord.equals("EE"))
  41. {
  42. end = 'Y';
  43. trigger = false;
  44. }
  45. if (endOfWord.equals("IE")) {
  46. end ='Y';
  47. trigger = false;
  48. }
  49. if (endOfWord.equals("DT") || endOfWord.equals("RT") || endOfWord.equals("RD") || endOfWord.equals("NT") || endOfWord.equals("ND")) {
  50. end ='D';
  51. trigger = false;
  52. }
  53.  
  54. if (endOfWord.equals("AY")) {
  55. end ='Y';
  56. trigger = false;
  57. }
  58. if (trigger == true) {
  59. useEnd = true;
  60. }
  61.  
  62. trigger = false;
  63. int middleLength = 0;
  64.  
  65. if (useEnd == true) {
  66. middleLength = s.length();
  67. }
  68. else {
  69. middleLength = s.length()-2;
  70. }
  71.  
  72. // Now the middle...
  73. for(int i = firstPartLength; i < middleLength; i++) {
  74. boolean lastResort = true;
  75.  
  76. if (secondTrigger == true) {
  77. System.out.println("I: " + i);
  78. secondTrigger = false;
  79. continue;
  80. }
  81.  
  82. if (trigger == true) {
  83. System.out.println("I: " + i);
  84. trigger = false;
  85. secondTrigger = true;
  86. continue;
  87. }
  88.  
  89. if (i < s.length() - 2) {
  90. String temp = s.substring(i, i+3);
  91. if (temp.equals("SCH")) {
  92. t += "SSS";
  93. trigger = true;
  94. }
  95. }
  96.  
  97. if (i < s.length() - 1) {
  98. String temp = s.substring(i, i+2);
  99.  
  100.  
  101. if (temp.substring(0,2).equals("PH")) {
  102. lastResort = false;
  103. t+= "FF";
  104. }
  105.  
  106. if (temp.substring(0,2).equals("KN")) {
  107. lastResort = false;
  108. t+= "NN";
  109. }
  110.  
  111. else if (temp.substring(0,1).equals("K")) {
  112. lastResort = false;
  113. t+= "C";
  114. }
  115.  
  116. if (temp.substring(0,2).equals("EV")) {
  117. lastResort = false;
  118. t+= "AF";
  119. }
  120.  
  121. else if (temp.substring(0,1).equals("A")) {
  122. lastResort = false;
  123. t+= "A";
  124. }
  125.  
  126. else if (temp.substring(0,1).equals("E")) {
  127. lastResort = false;
  128. t+= "A";
  129. }
  130.  
  131. else if (temp.substring(0,1).equals("I")) {
  132. lastResort = false;
  133. t+= "A";
  134. }
  135.  
  136. else if (temp.substring(0,1).equals("O")) {
  137. lastResort = false;
  138. t+= "A";
  139. }
  140.  
  141. else if (temp.substring(0,1).equals("U")) {
  142. lastResort = false;
  143. t+= "A";
  144. }
  145. }
  146.  
  147. String temp = s.substring(i, i+1);
  148.  
  149. if (temp.substring(0,1).equals("Q")) {
  150. lastResort = false;
  151. t+= "G";
  152. }
  153.  
  154. if (temp.substring(0,1).equals("Z")) {
  155. lastResort = false;
  156. t+= "S";
  157. }
  158.  
  159. if (temp.substring(0,1).equals("M")) {
  160. lastResort = false;
  161. t+= "N";
  162. }
  163.  
  164. if (temp.substring(0,1).equals("H")) {
  165. if (isNotVowel(s.charAt(i-1)) || isNotVowel(s.charAt(i+1))) {
  166. t+= s.charAt(i-1);
  167. lastResort = false;
  168. }
  169. }
  170.  
  171. if (temp.substring(0,1).equals("W")) {
  172. if (!isNotVowel(s.charAt(i-1))) {
  173. t+= s.charAt(i-1);
  174. lastResort = false;
  175. }
  176. }
  177.  
  178. if (lastResort == true) {
  179. if (t.length() > 0) {
  180. if (s.charAt(i) != t.charAt(t.length()-1)) {
  181. t+= s.charAt(i);
  182. }
  183. }
  184. else {
  185. t+=s.charAt(i);
  186. }
  187.  
  188. }
  189. }
  190.  
  191. // Add the end that we calculated before...
  192. if (useEnd == false) {
  193. t+=end;
  194. }
  195.  
  196. // Return our string...
  197. return t;
  198. }
  199.  
  200. // This method check to see if the character passed to it is not a vowel....
  201. public static boolean isNotVowel(char character) {
  202. if (character != 'A' && character != 'E' && character != 'I' && character != 'O' && character != 'U')
  203. return true;
  204. else {
  205. System.out.println("WE HAVE A VOWEL: " + character);
  206. return false;
  207. }
  208. }
  209.  
  210. public static void main(String args[]) {
  211. Scanner scan = new Scanner(System.in);
  212. System.out.print("Enter your word to convert to NYSIIS: ");
  213. String word = scan.nextLine();
  214.  
  215. System.out.println("Result: " + getNysiis(word));
  216. }
  217. }
Add Comment
Please, Sign In to add comment