Advertisement
Bradmasta

Name Game Code

Oct 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. import java.util.*;
  2. public class TheNameGameMain {
  3.  
  4.  
  5. public void TheNameGameMain() {
  6.  
  7. final int RETRY = 0;
  8. final int VOWELCHECK = 0;
  9. final int HIGHEST = 50;
  10. int counter = 0;
  11. String stringIn;
  12. String modStr;
  13. //loop makes sure criteria is met before exiting.
  14. while (RETRY >= 0) {
  15. System.out.println("Please Enter The Name >> ");
  16. Scanner newScan = new Scanner(System.in);
  17. try {
  18. stringIn = newScan.next();
  19. stringIn = stringIn.toLowerCase();
  20. boolean check;
  21. check = CheckName(stringIn);
  22. // This will check to make sure a word contains a vowel.
  23. if (check == false) {
  24.  
  25. System.out.println("The name has to contain a vowel!");
  26. System.out.println(" ");
  27. TheNameGameMain();
  28. }
  29. else {
  30. // This will prevent profanities from forming from the song itself.
  31. if (stringIn.contains("itch") || stringIn.contains("uck") || stringIn.contains("astard") || stringIn.contains("ucker") || stringIn.contains("ass")) {
  32.  
  33. System.out.println("Invalid name!");
  34. TheNameGameMain();
  35. }
  36. else {
  37. //Makes sure the word is either less than HIGHEST (which initialized at 50) or less than or equal to 3.
  38. if (stringIn.length() < HIGHEST && stringIn.length() >= 3) {
  39. // If a name starts with a vowel, the name doesn't need trimmed, and can proceed as is.
  40. if (stringIn.charAt(0) == 'a' || stringIn.charAt(0) == 'e' || stringIn.charAt(0) == 'i' || stringIn.charAt(0) == 'o' || stringIn.charAt(0) == 'u') {
  41. modStr = stringIn;
  42. Song(stringIn, modStr);
  43. break;
  44. }
  45. else {
  46.  
  47. while (counter <= stringIn.length()) {
  48. // Will loop until it finds the first vowel in the name. It will then trim off everything at and after the vowel and store it in "modStr"
  49. if (stringIn.charAt(counter) == 'a' || stringIn.charAt(counter) == 'e' || stringIn.charAt(counter) == 'i' || stringIn.charAt(counter) == 'o' || stringIn.charAt(counter) == 'u') {
  50. modStr = stringIn.substring(counter);
  51. Song(stringIn, modStr);
  52. break;
  53. }
  54. else {
  55. counter += 1;
  56. }
  57.  
  58.  
  59. }
  60. }
  61. }
  62. else {
  63. System.out.println("Name too long or too short!");
  64. TheNameGameMain();
  65. break;
  66. }
  67.  
  68. }
  69. }
  70.  
  71. }
  72. catch (InputMismatchException error) {
  73. System.out.println("Can't use nonsense here!");
  74. }
  75. break;
  76. }
  77. }
  78.  
  79.  
  80. //Method for checking to make sure a name entered contains a vowel.
  81. public static boolean CheckName(String word) {
  82.  
  83. boolean containsVowel = true;
  84. int foundA = word.indexOf('a');
  85. int foundE = word.indexOf('e');
  86. int foundI = word.indexOf('i');
  87. int foundO = word.indexOf('o');
  88. int foundU = word.indexOf('u');
  89.  
  90. if (foundA == -1 && foundE == -1 && foundI == -1 && foundO == -1 && foundU == -1) {
  91.  
  92. containsVowel = false;
  93. }
  94.  
  95. return containsVowel;
  96. }
  97.  
  98.  
  99. //Method for displaying the final song.
  100. public void Song(String stringIn, String modStr2) {
  101. String modStr1 = stringIn.substring(0, 1).toUpperCase() + stringIn.substring(1).toLowerCase();
  102.  
  103.  
  104. System.out.println(modStr1 + " " + modStr1 + " " + "Bo B" + modStr2);
  105. System.out.println("Banana Fana Fo F" + modStr2);
  106. System.out.println("Me Mi Mo M" + modStr2);
  107. System.out.println(modStr1);
  108. System.out.println(" ");
  109. RepeatGame();
  110.  
  111.  
  112. }
  113.  
  114. /*Method used to check if user would like to do it again, or quit. This method also makes sure the input is at least an integer,
  115. or will ask the user the question again*/
  116. public void RepeatGame() {
  117. final int RETRY = 0;
  118. int choice;
  119.  
  120. while (RETRY >= 0) {
  121. System.out.println("Would you like to play again?");
  122. System.out.println(" ");
  123. System.out.println("1) Yes!\n2) No, quit out");
  124. System.out.println(" ");
  125. Scanner newScan = new Scanner(System.in);
  126. try {
  127. choice = newScan.nextInt();
  128. if (choice == 1 || choice == 2) {
  129. RepeatGameChoice(choice);
  130. break;
  131. }
  132. else {
  133. System.out.println("Invalid choice! Try again :)");
  134. System.out.println(" ");
  135. RepeatGame();
  136. break;
  137. }
  138. }
  139. catch (InputMismatchException error) {
  140. System.out.println("Not a proper response!");
  141. }
  142. }
  143.  
  144. }
  145.  
  146. //method used that will confirm the users choice from the RepeatGame method.
  147. public void RepeatGameChoice(int choice) {
  148.  
  149. switch (choice) {
  150. case 1:
  151. TheNameGameMain();
  152. break;
  153.  
  154. case 2:
  155. System.out.println ("Thanks for playing!");
  156. break;
  157. default:
  158. System.out.println ( "Not valid! Try again!" );
  159. RepeatGame();
  160. break;
  161. }
  162.  
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement