Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:23:42+00:00 2026-06-09T14:23:42+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:23:43+00:00Added an answer on June 9, 2026 at 2:23 pm

    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());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an if statement, and I need to compare a single string with
When we compare the elements within the single Collection Object do we always need
I need to compare two dictionary values if the types stored are equal, this
I'd like to create a regular expression such that when I compare the a
I need to compare two account number columns from two different tables to see
Dear Programmers, I need to compare one column value to another column in single
I have a single page that I need to on occasion asynchronously check the
I need to compare particular content of 2 SQL tables located in different servers:
I need to compare two Unix timestamps and I'm having trouble with the math.
I need to compare the last string of a url with the id of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.