1

I need to compare single char to char array and see if array has that char.

My current code looks like this:

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

Edit Notes:

Really sorry for being confusing! I am just a Java Beginner =/
Basically I am writing small Hangman game with GUI.
My program reads off text file and randomly chooses word which player has to guess, then prints it out in hidden manner like this: _ _ _ _ _
In this case I want player to input character or string (person can guess either whole word or just one letter)
Then I want my program to take that letter or string and compare to my hidden word

Following code chooses word and hides it:

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

Then person input his character which he guesses to program with following code:

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.)

12
  • 5
    but you are comparing single string. Not a single char. Commented Aug 12, 2012 at 9:08
  • And function returns string. Not a char array Commented Aug 12, 2012 at 9:09
  • please provide more code, this method doesn't provide enough for NPE Commented Aug 12, 2012 at 9:10
  • Can you post some code where you use isThereChar()? Commented Aug 12, 2012 at 9:10
  • And you are comparing against a whole of array. Not element-wise Commented Aug 12, 2012 at 9:11

6 Answers 6

5

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

Sign up to request clarification or add additional context in comments.

Comments

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]) {

4 Comments

NullPointerException occurs only when I try to print boolean value to check if it is correct.
So the NPE is not occuring in the code above ... like I said. If you want us to explain the NPE, you will need to post the code that throws it.
Any ideas how to check what value it gives? Seems like I just checking it incorrectly.
Unless you show us the code and the stacktrace, we can't help you.
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());
        }
    }
}

16 Comments

maybe u got any idea how to check what it returns? trying to print it out to make sure answer is correct, but it just gives me nullpointer exceptions
You need to know what? The index of the found character? Where does it give a NPE? Have you checked that your char[] array isn't null?
Yeah I print it once I create it. I think I just dont know how to check value that this method returns. Any ideas?
I still don't get what you need. I'm gonna edit my answer with an example, to see if I can help you
Basically I want this method to check if char which was inputted by used is in my char array. If it is return true if it is not - false. Also I want to print out weather it is true or false for testing purposes. And I got stuck on that printing part.
|
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";
}

1 Comment

Since this is Java, you'd want to use chr.charAt(0)
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;
}

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.