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: String to Array?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default String to Array?

    OK so I actually have a couple basic problem but the most important problem is the array. I am making a hangman game. I have a driver that was supplied but it is actually confusing me more.

    1. Does the driver get the information from the file for me, or do I need to have the code that I put in main to get the information?

    2. I tried to output the String word just so that I knew file.next was working to get the information from the file, but I couldn't get it to output from either the displayGameIntro or play methods because it didn't recognize the variable. Any suggestions?

    3. This has to do with #2, since I can't seem to get the String word to be recognized, I can't turn it into an array. My plan is to create an array called letter (which I commented out while i was trying to get the other stuff working) that I can use when I am checking to see if the user has entered the correct letter. Is this the wrong idea to be using. Should I not use an array to check if the guessed letter is correct?

    Here is the driver:

    /* This program is a word guessing game called hangman.
     * A person will try and guess a word before the max 
     * number of guesses are used. Every time the user chooses an 
     * incorrect letter another body part is displayed in the gallows.                           
     */
     
    import java.util.*;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
     
    public class HangmanDriver {
     
        public static final String filename = "hw7data.txt";
     
        // Driver to run the game using the student Hangman class
        public static void main(String[] args) {
            Scanner wordsFile = null;                  // words data file
     
            // open the file containing the words
            try {
                wordsFile = new Scanner(new FileInputStream(filename));
            }
            catch (FileNotFoundException e) {
                System.out.println("File not found or not opened.");
                System.exit(0);
            }
     
            // create object and prepare to play game
            Hangman game = new Hangman(wordsFile);
            Scanner keyboard = new Scanner(System.in);
     
            // display an introduction on the game to the player
            game.displayGameIntro();
     
            // continually play new games if the user desires
            String playAgain;
            do {
                game.play();
                System.out.print("Do you want to play again? ");
                playAgain = keyboard.next();
                System.out.println();
            } while (playAgain.toUpperCase().startsWith("Y"));
     
            System.out.println("Thanks for playing!");
        }
    }


    This is my program:
    // This program runs a game of hangman
    // The point is to guess the word based on the number of blank spaces
    //
    //
    //@author: Kristen Watson
    //@version: 11/23/201
     
    import java.util.Scanner;
    import java.io.File;
     
    public class Hangman
    {  
       public final static String filename = "hw7data.txt";
     
       public static void main(String[] args) {
            Scanner inputFile = null;
            try {
                inputFile = new Scanner(new File(filename));
            } catch (Exception e) {
                System.out.println("File could not be opened: " + filename);
                System.exit(0);
            }
       }
     
       public Hangman(Scanner file) {
            // Store the file object in an instance variable for later use
            String word = file.next();
     
       }
     
        //Displays the Intro to the game for the player
        public void displayGameIntro() {
            System.out.println("You have just started a game of Hangman."); 
            System.out.println("You have seven guesses to figure out the correct word.");
            System.out.println("The blank spots indicate how long the word is. Each");
            System.out.println("correct letter will show up in the correct spot, while");
            System.out.println("a wrong guess will lead to another body part being");
            System.out.println("displayed. After seven incorrect guesses, you lose.");
     
        }
     
        public void play () {
            // Here is some high-level pseudocode of what you want to do:
            //
            // Initialize everything, such as arrays
            //int [] letters = new int [word.length];
            //for loop that takes word into array letters
            //for(int i = 0; i < word.length; i++){
            //    letters [i] = word[i];
            //
     
            // Get one word from the data file
            // Loop until game is over:
            //		Display current picture, letters guessed, wrong guesses, and word so far
            //		Get a valid, not-yet-guessed letter from the player
            //		Determine if the letter is in the word and update variables appropriately
            // Handle winning or losing
     
     
        }
     
    }
    Last edited by Norm; November 30th, 2012 at 09:50 AM. Reason: removed spaces in code tags

  2. The Following User Says Thank You to Kristenw17 For This Useful Post:

    tsharetlme (November 30th, 2012)


  3. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String to Array?

    What happens when you execute the code?

    What does the driver code pass to the Hangman constructor?

    I couldn't get it to output
    Is the method called? If it is called it should print out some messages. Do any of its messages get printed?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. Testing if an inputted String exists in an String Array
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2012, 03:46 PM
  3. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  4. How to convert String array to Integer array...?'
    By suyog53 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 25th, 2012, 08:50 AM
  5. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 10:24 PM