Java将单个char与char数组进行比较

时间:2012-08-12 09:05:33

标签: java arrays comparison char

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

我目前的代码如下:

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编写小型Hangman游戏 我的程序读取文本文件并随机选择哪个玩家必须猜测的单词,然后以隐藏的方式打印出来,如下所示:_ _ _ _ _
在这种情况下,我希望玩家输入字符或字符串(人可以猜出整个单词或只是一个字母)
然后我希望我的程序取出那个字母或字符串并与我隐藏的单词进行比较

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

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());
        }
    }

最后我想把那个输入的字符和我的字符数组进行比较,这是我隐藏的字:

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;
}

我想检查我的代码返回的内容(true或false),但我一直都没有这样做。 (现在我正试着在我的主课上调用方法来检查它,如果你能给我提示怎么做,否则请告诉我。)

6 个答案:

答案 0 :(得分:5)

我会使用:Chars.contains(array, chr);Guava Chars

答案 1 :(得分:3)

发生NullPointerException,因为调用方法时chaArraychrnull。 (如果没有,那么NullPointerException正在其他地方发生!!

您的代码的另一个问题是这一行:

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

由于chr实际上是一个String,所以这里会发生的是chaArray[i]的值将自动装箱为Character个对象,然后作为参数传递到String.equals(Object)。但String.equals(Object)将返回false,除非其参数为String ...因此您的代码无论如何都找不到该字符。

你需要比较这样的角色:

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

或声明chrchar并将其比较为:

  if (chr == chaArray[i]) {

答案 2 :(得分:1)

让我们看看我是否得到了你需要的东西:

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());
        }
    }
}

答案 3 :(得分:0)

String chr可能为空,导致NullPointerException

使用char chr代替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;
}

答案 4 :(得分:0)

从传入的参数中选择字符,或传入char,例如

chr[0]

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";
}

答案 5 :(得分: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
}