CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2019
    Posts
    4

    Question .contains() but for char ?

    Is there a way (in the while statement) to get something similar to s.contains(""); but for char values ?

    Code:
     while(blurredArray.contains('*')){        //Loop continues if asterisk is still there
    The question is:

    (Game: hangman) Write a hangman game that randomly generates a word and
    prompts the user to guess one letter at a time, as shown in the sample run. Each
    letter in the word is displayed as an asterisk. When the user makes a correct
    guess, the actual letter is then displayed. When the user finishes a word, display
    the number of misses and ask the user whether to continue to play with another
    word.

    (Guess) Enter a letter in word ******* > p
    (Guess) Enter a letter in word p****** > r
    (Guess) Enter a letter in word pr**r** > p
    p is already in the word
    (Guess) Enter a letter in word pr**r** > o
    (Guess) Enter a letter in word pro*r** > g
    (Guess) Enter a letter in word progr** > n
    n is not in the word
    (Guess) Enter a letter in word progr** > m
    (Guess) Enter a letter in word progr*m > a
    The word is program. You missed 1 time
    Do you want to guess another word? Enter y or n>

    Code:
    import java.util.*;
    
    public class Exercise_7_35 {
      public static void main(String[] args) {
        
        String[] array = {"lovely", "adorable", "hardworking", "consistence"};
        
        printArray(array);
        
      }
      
      public static void printArray(String[] array) {
        
        Scanner input = new Scanner(System.in);
        
        int random = (int)(Math.random() * array.length);  //Getting the random value to choose a word
        int i = 0;
        
        String guessArray = array[random];          //Initializing a random string to use for the game round
        
        char[] blurredArray = new char[guessArray.length()];
        
        for(i = 0; i < guessArray.length(); i++) {
          blurredArray[i] = '*';
        }
        
        while(blurredArray.contains('*')){        //Loop continues if asterisk is still there
          
          //Printing
          System.out.print("(Guess) Enter a letter in word ");
          
          for(i = 0; i < blurredArray.length; i++) {
            System.out.print(blurredArray[i]);
          }
          
          System.out.print(" > ");
          //Printing
          
          String userString = input.next();
          
          if(userString.length() != 1) {                      //If user tries to input more than 1 letter at a time
            System.out.println("One letter at a time!");
            System.exit(0);
          }
          
          char ch = array.charAt(i);
          
          if(isTrue(guessArray, userString, i)) {
            blurredArray[i] = ch;
          } else {
            System.out.println(ch + " is not in the word");
          }
        }
      }
      
      public static boolean isTrue(String guessArray, String userString, int i) {
        boolean bruh = true;
        
        char userChar = userString.charAt(0);
        
        if(userChar == guessArray.charAt(i)) {
          bruh = true;
        } else {
          bruh = false;
        }
        return bruh;
      }
      
    }
    That is the only wrong thing it shows me for now, not sure if im going to get more errors after lol.

    P.S mind that the code is incomplete! i'm just here for that .contain() part
    Last edited by universecloud; October 24th, 2019 at 01:07 PM.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: .contains() but for char ?

    arrays do not have methods.

    Why not write a simple method yourself?
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured