I have made a simple word scrambling game - and while it works, I need to separate it into methods. I have tried multiple different things and none of them seem to work. Any suggestions are welcome. Here is my code:
import java.util.Scanner; import java.io.*; public class WordScrambler{ Scanner userInput = new Scanner(System.in); final static int numOfChoices = 4; final static String choice1 = "Sports"; final static String choice2 = "Clothing"; final static String choice3 = "Subjects"; final static String choice4 = "Animals"; public static void run() throws FileNotFoundException{ Scanner userInput = new Scanner(System.in); System.out.println("Hi! Welcome to Wonders with Words :) Are you ready to unscramble? Type Yes or No"); String areYouReady = userInput.next(); if (areYouReady.indexOf("Yes") > -1){ System.out.println("Which category would you like to play? You have: " + numOfChoices + " choices: " + choice1 + "," + choice2 + "," + choice3 + " or " + choice4 + "."); String userChoice = userInput.next(); if(userChoice.indexOf("Sports") > -1){ Scanner newFileInput = new Scanner(new File("sports.txt")); int sizeOfArray = 0; while(newFileInput.hasNextLine()){ String input = newFileInput.nextLine(); sizeOfArray++; }//end while newFileInput.close(); newFileInput = new Scanner(new File("sports.txt")); String[] words = new String[sizeOfArray]; for (int i = 0; i<words.length; i++) words [i] = newFileInput.next(); newFileInput.close(); String userGuess = ""; int correctAns = 0; int wrongAns = 0; String[] scrambledWords = new String[sizeOfArray]; for (int i = 0; i<words.length; i++) scrambledWords[i] = words[i].substring(1, 3) + words[i].substring(5, words[i].length()) + words[i].substring(0,1) + words[i].substring(3, 5); for(int i = 0; i<words.length; i++){ System.out.println("Your word is: " + scrambledWords[i]); userGuess = userInput.next(); if(words[i].indexOf(userGuess) > -1) correctAns++; else { wrongAns++; System.out.println("The correct answer is: " + words[i]); }//end else }//end for System.out.println("You got " + correctAns + " words right!"); }//end if else if(userChoice.indexOf("Clothing") > -1){ Scanner newFileInput = new Scanner(new File("clothing.txt")); int sizeOfArray = 0; while(newFileInput.hasNextLine()){ String input = newFileInput.nextLine(); sizeOfArray++; }//end while newFileInput.close(); newFileInput = new Scanner(new File("clothing.txt")); String[] words = new String[sizeOfArray]; for (int i = 0; i<words.length; i++) words [i] = newFileInput.next(); newFileInput.close(); String userGuess = ""; int correctAns = 0; int wrongAns = 0; String[] scrambledWords = new String[sizeOfArray]; for (int i = 0; i<words.length; i++) scrambledWords[i] = words[i].substring(1, 3) + words[i].substring(5, words[i].length()) + words[i].substring(0,1) + words[i].substring(3, 5); for(int i = 0; i<words.length; i++){ System.out.println("Your word is: " + scrambledWords[i]); userGuess = userInput.next(); if(words[i].indexOf(userGuess) > -1) correctAns++; else { wrongAns++; System.out.println("The correct answer is: " + words[i]); }//end else }//end for System.out.println("You got " + correctAns + " words right!"); }//end if else if(userChoice.indexOf("Subjects") > -1){ Scanner newFileInput = new Scanner(new File("subjects.txt")); int sizeOfArray = 0; while(newFileInput.hasNextLine()){ String input = newFileInput.nextLine(); sizeOfArray++; }//end while newFileInput.close(); newFileInput = new Scanner(new File("subjects.txt")); String[] words = new String[sizeOfArray]; for (int i = 0; i<words.length; i++) words [i] = newFileInput.next(); newFileInput.close(); String userGuess = ""; int correctAns = 0; int wrongAns = 0; String[] scrambledWords = new String[sizeOfArray]; for (int i = 0; i<words.length; i++) scrambledWords[i] = words[i].substring(1, 3) + words[i].substring(5, words[i].length()) + words[i].substring(0,1) + words[i].substring(3, 5); for(int i = 0; i<words.length; i++){ System.out.println("Your word is: " + scrambledWords[i]); userGuess = userInput.next(); if(words[i].indexOf(userGuess) > -1) correctAns++; else { wrongAns++; System.out.println("The correct answer is: " + words[i]); }//end else }//end for System.out.println("You got " + correctAns + " words right!"); }//end if else if(userChoice.indexOf("Animals") > -1){ Scanner newFileInput = new Scanner(new File("animals.txt")); int sizeOfArray = 0; while(newFileInput.hasNextLine()){ String input = newFileInput.nextLine(); sizeOfArray++; }//end while newFileInput.close(); newFileInput = new Scanner(new File("animals.txt")); String[] words = new String[sizeOfArray]; for (int i = 0; i<words.length; i++) words [i] = newFileInput.next(); newFileInput.close(); String userGuess = ""; int correctAns = 0; int wrongAns = 0; String[] scrambledWords = new String[sizeOfArray]; for (int i = 0; i<words.length; i++) scrambledWords[i] = words[i].substring(1, 3) + words[i].substring(5, words[i].length()) + words[i].substring(0,1) + words[i].substring(3, 5); for(int i = 0; i<words.length; i++){ System.out.println("Your word is: " + scrambledWords[i]); userGuess = userInput.next(); if(words[i].indexOf(userGuess) > -1) correctAns++; else { wrongAns++; System.out.println("The correct answer is: " + words[i]); }//end else }//end for System.out.println("You got " + correctAns + " words right!"); }//end if else; }//end if }//end main }//end class