Results 1 to 6 of 6
  1. #1
    larsjuh is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default System.in.read(); to a char array

    Hello,

    I've experienced a little problem while running my code... Im trying to get keyboard input stored in an array (char)


    Java Code:
    package practicum5;
    
    public class GuessedLetters {
        public char data[] = new char[5];
        int iter = 0;
        
      
        public void print() {
           System.out.println("Guessed Letters:");
            for (int i = 0; i < data.length; i++) {
                System.out.println("[" + data[i] + "]");
            }
    
        }
        
        public void getCh() throws java.io.IOException {
               System.out.println("Next Letter:");
            data[iter] = (char) System.in.read();      
            iter++;
                                  
        }
    }
    The thing is (it does work), but it skips one array everytime.. so it looks like this. (a)()(b)()(c).
    I can't seem to understand why...


    My main code:

    Java Code:
    package practicum5;
    
    import java.io.IOException;
    
    public class Practicum5 {
    
        public static void main(String[] args) throws IOException {
    
            Galg galg = new Galg();
            Word word = new Word();
            GuessedLetters guessedLetters = new GuessedLetters();
            int missers = 0;
    
    
    
            while (word.hasBeenGuessed() == false || missers < galg.Galgje.length) {
    
                galg.welkeGalg(missers);
                word.print();
    
                guessedLetters.getCh();
                guessedLetters.print();
    
    
            }
    
        }
    }
    I really hope i can find an answer here :D
    Thanks for your time.

    Greetings.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: System.in.read(); to a char array

    What is Galg? What is Word?

  3. #3
    larsjuh is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: System.in.read(); to a char array

    Hi,

    Word class

    Java Code:
    package practicum5;
    
    /**
     * Word is the class used to store and represent
     * a word for the game Hangman. The class only
     * supports a single word; "hangman".
     * 
     * @author Ralph Benjamin Ruijs
     */
    public class Word {
    
        // The word that has to be guessed
        private char[] word;
    
        /* An array denoting whether the corresponding character
        has been guessed.
        guessed[i] == true means that word[i] has been guessed */
        private boolean[] guessed;
    
        /**
         * Create a new instance of this class.
         * The word will be set to "hangman".
         */
        public Word() {
            word = "hangman".toCharArray(); // convert the string to an array
            guessed = new boolean[word.length]; // default initialised to false
        }
    
        /**
         * Print the word to System.out formatted like:
         * h.n...n
         */
        public void print() {
            for (int i = 0; i < word.length; i++) {
                if (guessed[i]) {
                    System.out.print(word[i]); // char is guessed, so print it
                } else {
                    System.out.print('.'); // char is not guessed yet, so hide it
                }
            }
            System.out.println();
        }
    
        /**
         * Process a guess (character) against the word. If the
         * character occurs in the word, its corresponding
         * boolean will be set to true.
         *
         * @param guess, the character to process
         * @return true if the character occurs in the word,
         *         false otherwise
         */
        public boolean processGuess(char guess) {
            boolean success = false;
            for (int i = 0; i < word.length; i++) {
                char ch = word[i]; // for every char in the word
                if (ch == guess) { // check if char matches the guess
                    guessed[i] = true;
                    success = true;
                }
            }
            return success;
        }
    
        /**
         * Find out if all the letters of the word have
         * been guessed.
         * @return true if the whole word has been guessed,
         *         false otherwise
         */
        public boolean hasBeenGuessed() {
            for (boolean charGuessed : guessed) {
                if (!charGuessed) { // if one of the chars has not been guessed
                    return false; // complete word has not been guessed yet
                }
            }
            return true; // all chars have been guessed -> word has been guessed
        }
    }
    And the galg class:

    Java Code:
    package practicum5;
    
    public class Galg {
    
      
        String[] Galgje = {"|\n|\n|\n| _ _ ", "_____\n|\n|\n|\n| _ _", "_____\n|/\n|\n|\n| _ _", "_____\n|/  |\n|   O\n|\n| _ _", "_____\n|/  |\n|   O\n|   |\n|\n| _ _", "_____\n|/  |\n|   O\n|  /|\n|\n| _ _",
            "_____\n|/  |\n|   O\n|  /|\\\n|\n| _ _", "_____\n|/  |\n|   O\n|  /|\\\n|  /\n| _ _", "_____\n|/  |\n|   O\n|  /|\\\n|  / \\\n| _ _"};
    
        public void printAllegalgjes() {
            for (String galg : Galgje) {
                System.out.println(galg);
            }
        }
    
        public void welkeGalg(int fout) {
            System.out.println(Galgje[fout]);
    
        }
    }
    Im trying to make hangman =)

  4. #4
    larsjuh is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: System.in.read(); to a char array

    Galg Class:

    Java Code:
    package practicum5;
    
    public class Galg {
    
      
        String[] Galgje = {"|\n|\n|\n| _ _ ", "_____\n|\n|\n|\n| _ _", "_____\n|/\n|\n|\n| _ _", "_____\n|/  |\n|   O\n|\n| _ _", "_____\n|/  |\n|   O\n|   |\n|\n| _ _", "_____\n|/  |\n|   O\n|  /|\n|\n| _ _",
            "_____\n|/  |\n|   O\n|  /|\\\n|\n| _ _", "_____\n|/  |\n|   O\n|  /|\\\n|  /\n| _ _", "_____\n|/  |\n|   O\n|  /|\\\n|  / \\\n| _ _"};
    
        public void printAllegalgjes() {
            for (String galg : Galgje) {
                System.out.println(galg);
            }
        }
    
        public void welkeGalg(int fout) {
            System.out.println(Galgje[fout]);
    
        }
    }
    Word Class:

    Java Code:
    package practicum5;
    
    /**
     * Word is the class used to store and represent
     * a word for the game Hangman. The class only
     * supports a single word; "hangman".
     * 
     * @author Ralph Benjamin Ruijs
     */
    public class Word {
    
        // The word that has to be guessed
        private char[] word;
    
        /* An array denoting whether the corresponding character
        has been guessed.
        guessed[i] == true means that word[i] has been guessed */
        private boolean[] guessed;
    
        /**
         * Create a new instance of this class.
         * The word will be set to "hangman".
         */
        public Word() {
            word = "hangman".toCharArray(); // convert the string to an array
            guessed = new boolean[word.length]; // default initialised to false
        }
    
        /**
         * Print the word to System.out formatted like:
         * h.n...n
         */
        public void print() {
            for (int i = 0; i < word.length; i++) {
                if (guessed[i]) {
                    System.out.print(word[i]); // char is guessed, so print it
                } else {
                    System.out.print('.'); // char is not guessed yet, so hide it
                }
            }
            System.out.println();
        }
    
        /**
         * Process a guess (character) against the word. If the
         * character occurs in the word, its corresponding
         * boolean will be set to true.
         *
         * @param guess, the character to process
         * @return true if the character occurs in the word,
         *         false otherwise
         */
        public boolean processGuess(char guess) {
            boolean success = false;
            for (int i = 0; i < word.length; i++) {
                char ch = word[i]; // for every char in the word
                if (ch == guess) { // check if char matches the guess
                    guessed[i] = true;
                    success = true;
                }
            }
            return success;
        }
    
        /**
         * Find out if all the letters of the word have
         * been guessed.
         * @return true if the whole word has been guessed,
         *         false otherwise
         */
        public boolean hasBeenGuessed() {
            for (boolean charGuessed : guessed) {
                if (!charGuessed) { // if one of the chars has not been guessed
                    return false; // complete word has not been guessed yet
                }
            }
            return true; // all chars have been guessed -> word has been guessed
        }
    }

  5. #5
    larsjuh is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: System.in.read(); to a char array

    Im trying to make hangman, and the Word class has several methods in it, but i don't really make use of it now..
    Only word.print(); , and this prints "......." and if a letter has been guessed it prints like this ".....a..."

    And galg is a class that have the hangman figures in it..

    galg.welkeGalg(missers); - missers is an integer that starts at 0. (so it prints figure 1 of the hangman) missers++ prints figure 2 of the hangman.. etc etc.
    Last edited by larsjuh; 10-21-2011 at 03:25 PM.

  6. #6
    larsjuh is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 12:32 PM
  2. Replies: 2
    Last Post: 03-08-2011, 07:20 AM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 10:01 PM
  4. How to read a Char character ?!
    By Sary in forum New To Java
    Replies: 4
    Last Post: 03-27-2010, 07:01 PM
  5. read char words lines
    By Drukqs in forum New To Java
    Replies: 6
    Last Post: 05-14-2009, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •