1

我需要将单个字符与字符数组进行比较,看看数组是否具有该字符。

我当前的代码如下所示:

public boolean isThereChar(char[] chaArray, String chr){
    boolean bool = false;
    for(int i=0; i < chaArray.length; i++)
            {
                if(chr.equals(chaArray[i])){
                    bool = true;
                }
            }
            return bool;
}

编辑注释:

真的很抱歉造成混淆!我只是一个 Java 初学者 =/
基本上我正在用 GUI 编写小型刽子手游戏。
我的程序读取文本文件并随机选择玩家必须猜测的单词,然后以这样的隐藏方式打印出来:_ _ _ _ _
在这种情况下,我希望玩家输入字符或字符串(人们可以猜测整个单词或只有一个字母)
然后我希望我的程序获取那个字母或字符串并与我的隐藏词进行比较

以下代码选择单词并将其隐藏:

public String pickWord(){
    String guessWord = (wordsList[new Random().nextInt(wordsList.length)]);
    return guessWord.toLowerCase();
}

//Hides picked word
public char[] setWord(){
    char[] word = new char[pickWord().length() * 2];
    for (int i = 0; i < word.length; i+=2) {
        word[i] = '_';
        word[i + 1] = ' ';
    }
    return word;
}

然后人们输入他猜想用以下代码编程的角色:

public void actionPerformed(ActionEvent e) {
    String action = e.getActionCommand();

    if (action == "Guess Letter"){
        inputChar = JOptionPane.showInputDialog("Please enter letter (a-z)");
        if (inputChar.length() > 1){
            GuessedLetters glr = new GuessedLetters(inputChar);
            glr.setInString(inputChar);
            //For testing purposes
            System.out.println("This is String: " +glr.getInString());              
        }else{
        GuessedLetters glr = new GuessedLetters(inputChar);
        glr.setInChar(inputChar);
        //For testing purposes
        System.out.println("This is Char: " +glr.getInChar());
        }
    }

Lastly I want to take that character which was inputted and compare to my array of chars which is my hidden word:

public boolean isThereChar(char[] array, String str){
    return isThereChar(array, str.charAt(0));
}

public boolean isThereChar(char[] array, char c){
    for(int i=0; i<array.length; i++){
        if (array[i] == c) return true;
    }
    return false;
}

I want to check what does my code returns (true or false), but I keep failing at doing so. (Right now I am trying to call method in my main class to check it, if you can give me tips how to do it otherwise please let me know.)

4

6 回答 6

5

I would use: Chars.contains(array, chr); with Guava Chars

于 2012-08-12T09:20:20.603 回答
3

The NullPointerException is happening because either chaArray or chr is null when you call the method. (And if not, then the NullPointerException is occurring somewhere else!!)

The other problem with your code is this line:

  if (chr.equals(chaArray[i])) {

Since chr is actually a String, what is going to happen here is that the value of chaArray[i] will be auto-boxed as a Character object, and then passed as an argument to String.equals(Object). But the String.equals(Object) will return false unless its argument is a String ... so your code wouldn't find the character anyway.

You need to either compare the character like this:

  if (chr.charAt(0) == chaArray[i]) {

or declare chr to be a char and compare it as:

  if (chr == chaArray[i]) {
于 2012-08-12T09:17:27.747 回答
1

Let's see if I got what you need :

public void actionPerformed(ActionEvent e) {
    String action = e.getActionCommand();
    if (action == "Guess Letter"){
        inputChar = JOptionPane.showInputDialog("Please enter letter (a-z)");
        if (inputChar.length() > 1){ //User input is a string here, right?
            GuessedLetters glr = new GuessedLetters(inputChar);
            glr.setInString(inputChar);
            System.out.println(wordToGuess.contains(glr.getInString())); //This will print true if wordToGuess is equal to glr.getInString() or if it just contains it
            //For testing purposes
            System.out.println("This is String: " +glr.getInString());              
        }else{ //Here the user gave us just a character, so we've got to know if this character is contained in the word, right?
        GuessedLetters glr = new GuessedLetters(inputChar);
        glr.setInChar(inputChar);
        System.out.println(wordToGuess.contains(glr.getInChar()); //This will print true if your char is in the wordToGuess string
        //For testing purposes
        System.out.println("This is Char: " +glr.getInChar());
        }
    }
}
于 2012-08-12T09:13:51.790 回答
0

String chr might be null causing NullPointerException.

Use char chr instead of String.

public boolean isThereChar(char[] chaArray, char chr){
    boolean bool = false;
    for(int i=0; i < chaArray.length; i++) {
        if(chr==chaArray[i])){
             bool = true;
        }
    }
    return bool;
}
于 2012-08-12T09:10:53.400 回答
0

Select the character from the parameter passed in, or pass in a char e.g.

chr[0]

or

public String isThereChar(char[] chaArray, char chr){
    for(int i=0; i < chaArray.length; i++)
            {
                if(chr.equals(chaArray[i])){
                    return chr;
                }
            }
            return "Guess Again";
}
于 2012-08-12T09:12:45.763 回答
0
public boolean  isThereChar(char[] chaArray, char chr){        
for(int i=0; i < chaArray.length; i++)
        {
            if((chaArray[i]==chr)){
                return true;   // means Character exist in the Character array
            }
        }
        return false;  //// means Character does not exist in the Character array
}
于 2012-08-12T09:22:30.337 回答