Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Word Scrambling Game Method Help!

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Word Scrambling Game Method Help!

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,955
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Java Word Scrambling Game Method Help!

    What kind of tasks does the code do? Methods usually do something. Find a task and move the code that does it into a method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 02:14 PM
  2. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  3. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 04:23 AM
  4. Scrambling a String
    By Dylan035 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 13th, 2010, 08:33 PM
  5. im in a hurry!!Help with a programm..java game of guessing a word
    By mr_doctor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:17 AM

Tags for this Thread