Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class TheNameGameMain {
- public void TheNameGameMain() {
- final int RETRY = 0;
- final int VOWELCHECK = 0;
- final int HIGHEST = 50;
- int counter = 0;
- String stringIn;
- String modStr;
- //loop makes sure criteria is met before exiting.
- while (RETRY >= 0) {
- System.out.println("Please Enter The Name >> ");
- Scanner newScan = new Scanner(System.in);
- try {
- stringIn = newScan.next();
- stringIn = stringIn.toLowerCase();
- boolean check;
- check = CheckName(stringIn);
- // This will check to make sure a word contains a vowel.
- if (check == false) {
- System.out.println("The name has to contain a vowel!");
- System.out.println(" ");
- TheNameGameMain();
- }
- else {
- // This will prevent profanities from forming from the song itself.
- if (stringIn.contains("itch") || stringIn.contains("uck") || stringIn.contains("astard") || stringIn.contains("ucker") || stringIn.contains("ass")) {
- System.out.println("Invalid name!");
- TheNameGameMain();
- }
- else {
- //Makes sure the word is either less than HIGHEST (which initialized at 50) or less than or equal to 3.
- if (stringIn.length() < HIGHEST && stringIn.length() >= 3) {
- // If a name starts with a vowel, the name doesn't need trimmed, and can proceed as is.
- if (stringIn.charAt(0) == 'a' || stringIn.charAt(0) == 'e' || stringIn.charAt(0) == 'i' || stringIn.charAt(0) == 'o' || stringIn.charAt(0) == 'u') {
- modStr = stringIn;
- Song(stringIn, modStr);
- break;
- }
- else {
- while (counter <= stringIn.length()) {
- // 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"
- if (stringIn.charAt(counter) == 'a' || stringIn.charAt(counter) == 'e' || stringIn.charAt(counter) == 'i' || stringIn.charAt(counter) == 'o' || stringIn.charAt(counter) == 'u') {
- modStr = stringIn.substring(counter);
- Song(stringIn, modStr);
- break;
- }
- else {
- counter += 1;
- }
- }
- }
- }
- else {
- System.out.println("Name too long or too short!");
- TheNameGameMain();
- break;
- }
- }
- }
- }
- catch (InputMismatchException error) {
- System.out.println("Can't use nonsense here!");
- }
- break;
- }
- }
- //Method for checking to make sure a name entered contains a vowel.
- public static boolean CheckName(String word) {
- boolean containsVowel = true;
- int foundA = word.indexOf('a');
- int foundE = word.indexOf('e');
- int foundI = word.indexOf('i');
- int foundO = word.indexOf('o');
- int foundU = word.indexOf('u');
- if (foundA == -1 && foundE == -1 && foundI == -1 && foundO == -1 && foundU == -1) {
- containsVowel = false;
- }
- return containsVowel;
- }
- //Method for displaying the final song.
- public void Song(String stringIn, String modStr2) {
- String modStr1 = stringIn.substring(0, 1).toUpperCase() + stringIn.substring(1).toLowerCase();
- System.out.println(modStr1 + " " + modStr1 + " " + "Bo B" + modStr2);
- System.out.println("Banana Fana Fo F" + modStr2);
- System.out.println("Me Mi Mo M" + modStr2);
- System.out.println(modStr1);
- System.out.println(" ");
- RepeatGame();
- }
- /*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,
- or will ask the user the question again*/
- public void RepeatGame() {
- final int RETRY = 0;
- int choice;
- while (RETRY >= 0) {
- System.out.println("Would you like to play again?");
- System.out.println(" ");
- System.out.println("1) Yes!\n2) No, quit out");
- System.out.println(" ");
- Scanner newScan = new Scanner(System.in);
- try {
- choice = newScan.nextInt();
- if (choice == 1 || choice == 2) {
- RepeatGameChoice(choice);
- break;
- }
- else {
- System.out.println("Invalid choice! Try again :)");
- System.out.println(" ");
- RepeatGame();
- break;
- }
- }
- catch (InputMismatchException error) {
- System.out.println("Not a proper response!");
- }
- }
- }
- //method used that will confirm the users choice from the RepeatGame method.
- public void RepeatGameChoice(int choice) {
- switch (choice) {
- case 1:
- TheNameGameMain();
- break;
- case 2:
- System.out.println ("Thanks for playing!");
- break;
- default:
- System.out.println ( "Not valid! Try again!" );
- RepeatGame();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement