简体   繁体   English

Java中charAt()方法的问题

[英]Problems with charAt() method in Java

I am trying to make a text-based Hangman in Java. 我正在尝试用Java创建一个基于文本的Hangman。

This is my Java code: 这是我的Java代码:

package hangman;

import java.util.Random;
import java.util.Scanner;
import java.lang.String;

public class Hangman {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner chez = new Scanner(System.in);
        Scanner waffle = new Scanner(System.in);
        Random randomGenerator = new Random();
        StringBuilder displayWord = new StringBuilder("______________________________");
        String theWord = null;
        String preLetter;
        //String sbDisplayWord;
        char letter = 0;
        int randomNumber;
        int lengthOfWord;
        int numberOfLetterInWord = 0;
        int gDisplay;
        int guessWordNumber = 0;
        String guessWord;
        RandomWord troll = new RandomWord();    
        randomNumber = randomGenerator.nextInt(12);
        //Fill var with the word.
        theWord = troll.wordDecide(randomNumber);

        System.out.println ("Welcome to Hangman!");

        lengthOfWord=theWord.length( );
        System.out.println("This word has " + lengthOfWord + " letters.");

        System.out.println("You have 20 guesses.");

        for (int g =19; g >= 0; g--) {    

            System.out.println("If you want to guess the word, type 0. If you want to guess a letter, type 1.");
            guessWordNumber=chez.nextInt();

            if (guessWordNumber==0) {
                System.out.println("Enter the word now. Remember, don't capitalize it.");
                guessWord=waffle.nextLine();
                if (guessWord.equals(theWord)) {
                    System.out.println("YOU WIN");
                    System.exit(0);
                } else {
                    System.out.println("Sorry, this wasn't the correct word.");
                }
            } else if (guessWordNumber==1) {

                System.out.println("Please enter the letter you wish to guess with.");
                //System.out.println("It will tell you if you have guessed right for any of the letters. If it is blank, that means none of the letters match.");
                preLetter=chez.nextLine();
                letter=preLetter.charAt(0);
                System.out.println("");
                for(int i = 0; i <= lengthOfWord -1; i++ ) {     //-Eshan

                    if (letter == theWord.charAt( i )) {

                        numberOfLetterInWord=i+1;
                        System.out.println("This letter matches with letter number " + numberOfLetterInWord + " in the word.");
                        displayWord.setCharAt(i, letter);
                    } else {

                        numberOfLetterInWord=i+1;
                        System.out.println("This letter doesn't match with letter number " + numberOfLetterInWord + " in the word.");

                    }       


                }

                System.out.println("");
                System.out.println("The word so far is " + displayWord);
                System.out.println("");
                gDisplay = g + 1;
                System.out.println("You have " + gDisplay + " guesses left.");

            } else {
                System.exit(0);
            }

        }

        System.out.println("GAME OVER");
        System.exit(0);

    }
}



package hangman;

public class RandomWord {
    private static String[] wordArray = {
        "psychology",
        "keratin",
        "nostalgia",
        "pyromaniac",
        "chlorophyl",
        "derivative",
        "unitard",
        "pterodactyl",
        "xylophone",
        "excommunicate",
        "obituary",
        "infinitesimal",
        "concupiscent",
    };

    public String wordDecide(int randomNumber) {
        String theWord;
        theWord = wordArray[randomNumber];
        return theWord;
    }

}

Netbeans is giving me this error: Netbeans给了我这个错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0
at java.lang.String.charAt(String.java:695) at java.lang.String.charAt(String.java:695)
at hangman.Hangman.main(Hangman.java:56) 在hangman.Hangman.main(Hangman.java:56)
Java Result: 1 Java结果:1

当您在长度为0的字符串上调用charAt(0)时,可能会发生这种情况。在调用方法之前,应检查字符串是否为空。

You are getting a StringIndexOutOfBoundsException due to the fact the line 由于该行的原因,您将获得StringIndexOutOfBoundsException

guessWordNumber = chez.nextInt();

does not consume newline characters and passes the character through to the line 不消耗换行符并将字符传递给该行

preLetter = chez.nextLine();

which then doesn't block as it will have already received input. 然后它不会阻止,因为它已经收到输入。 This assigns an empty String to preLetter resulting in the exception. 这会将一个空String分配给preLetter从而导致异常。 You can use Scanner#nextLine to consume this character: 您可以使用Scanner#nextLine来使用此字符:

guessWordNumber = Integer.parseInt(chez.nextLine());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM