4 Replies - 249 Views - Last Post: 26 May 2020 - 02:15 PM Rate Topic: -----

#1 daberace   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-April 20

I dont know why my code stops after the user enters a phrase wrong

Posted 16 May 2020 - 01:16 PM

I am trying to create a wheel of fortune for my assignment. Every time I enter a constant it would return as false. (the user will never be able to guess the correct answer). Also how do you out "----" to indicate how many letters are in the word.
If someone can please help me with this
Here is my code:

package oops;

import java.util.Random;
import java.util.*;

public class Fortune
{
   
  private static final String Array = null;
public static char[] phraseToArray(String newWord)
  {
    char[] array = new char [newWord.length()];
    for (int index = 0; index < newWord.length(); index++)
    {
      array[index] = newWord.charAt(index);
    }
    return array;
  }
  
  public static char[] phraseArrayToBlank (char[] phraseArray)
  {
    char[] hiddenArray = new  char [phraseArray.length];
    for (int index = 0; index < phraseArray.length; index++)
    {
      if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf (Character.toUpperCase(phraseArray[index])) >= 0)
        hiddenArray[index] = '_';
      else
        hiddenArray[index] = phraseArray[index];
    }
    return hiddenArray;
  }
  

  public static String letterRemove (String letterStr, char letter)
  {
    String letterStrRemove = "";
    int findIndex = letterStr.indexOf (Character.toUpperCase(letter));
    letterStrRemove = letterStr.substring(0,findIndex) + letterStr.substring(findIndex+1); 
    return letterStrRemove;
  }
  
   public static boolean isGuessCorrect (char[] phraseArray, char guess)
  {
    boolean correct = false;
    for (int index = 0; index < phraseArray.length; index++)
    {
      if (phraseArray[index] == (Character.toUpperCase(guess))) //if the guess is found in the char array
      {
        correct =  true;
        break;
      }
      else
        correct =  false;
    }
    if (correct == true)
      return true; //return true
    else
      return false; //return false
  }
/***************************************************************************************************************/       
    public static char[] revealLetter (char[] phraseArray, char[] phraseArrayHidden,char guess)
  {
    for (int index = 0; index < phraseArrayHidden.length; index++)
    {
      if (phraseArray[index] == Character.toUpperCase(guess))
        phraseArrayHidden[index] = phraseArray[index];
    }
    return phraseArrayHidden;
  }
/***************************************************************************************************************/        
    public static boolean isPhraseSolved (char[] phraseArray, char[] phraseArrayHidden)
  {
    boolean playerWin = false;
    for (int index = 0; index <phraseArrayHidden.length; index++)
    {
      if (Character.toUpperCase(phraseArrayHidden[index]) == (phraseArray[index]))
        playerWin = true;
      else
      {
        playerWin =  false;
        break;
      }
    }
    if (playerWin == true)
      return true;
    else 
      return false;
  }
/***************************************************************************************************************/    
/***************************************************************************************************************/       
public static void main (String [] args)
  {
    Scanner input = new Scanner (System.in);
    String newWord = " ";
    
      
      int score = 0;
      int option = 0;
     
      char guess = 0;
      int randomm = 0;
      String phrase = "";;
      String consonantStr = "BCDFGHJKLMNPQRSTVWXYZ";
      String vowelStr = "AEIOU";
      String solveGuess = "";
 
      
     
      //boolean restartCheck = true;
      boolean isNotConsonant = false;
      boolean isNotVowel = false;
      boolean guessCorrect = false;
      boolean playerWin = false;
      boolean enoughPoints = false;
   
      System.out.println ("Welcome to the Wheel of Fortune game!");
 
      Random rand = new Random();
      
      int upperbound = 5;
      int int_random = rand.nextInt(upperbound); 
      
      String[] topics = new String[] {"ANIMALS", "PLANETS" , "SPORTS" , "FRUIT" , "MOVIE"};
    String topic = topics[int_random];
  
      System.out.println(" CATEGORY:               " +topic);
      

 
     
 String[] animals = new String[] {"ELEPHANT", "GIRAFFE" , "DOLPHIN" , "MONEY" , "PANDA"};

 String[] planets = new String[] {"VENUS", "MERCURY" , "EARTH" , "MARS" , "JUPITER"};
 
 String[] sports = new String[] {"HOCKEY", "BASEBALL" , "BASKETBALL" , "VOLLEYBALL" , "LACROSSE"};

 String[] fruit = new String[] {"APPLE", "PEAR" , "PINEAPPLE" , "GRAPE" , "MANGO"};

 String[] movie = new String[] {"harry potter", "us" , "avengers" , "Iron man" , "Batman"};
 
      String word = "";
      
      if (topic == "ANIMALS"){
      
      
      int animal_random = rand.nextInt(upperbound); 
      
      word = animals[animal_random];    
      }
      else if (topic == "PLANETS"){
      
      
      int planet_random = rand.nextInt(upperbound); 
      word = planets[planet_random];    
      }
      else if (topic == "SPORTS"){
      
      int sport_random = rand.nextInt(upperbound);
      
      word = sports[sport_random];  
      }
      else if (topic == "FRUIT"){
      
      int fruit_random = rand.nextInt(upperbound);
      
      word = fruit[fruit_random];    
      }
      else if (topic == "MOVIE"){
      
      int movie_random = rand.nextInt(upperbound);
      
      word = movie[movie_random];    
      }
   System.out.println(" SECRET WORD:           " +word);
 
      phrase = newWord;    
      char[] phraseArray = new char[phrase.length()];
      phraseArray = phraseToArray (phrase);     
      char[] phraseArrayHidden = new char[phraseArray.length];     
      phraseArrayHidden = phraseArrayToBlank (phraseArray);
     
      
  double randomNumber = (Math.random() * 4) + 1;
 
 

  
     System.out.println (randomm);
     for (int index = 0; index < phraseArrayHidden.length; index++)
      {
        System.out.print (phraseArrayHidden[index] + " ");
      }

      System.out.println ("Score: " + score);
   
      
     do{

        System.out.println ("\n\n1. Guess a Consonant");
        System.out.println ("2. Buy a Vowel (Cost: 250)");
        System.out.println ("3. Solve");
        option = input.nextInt();
        
        if (option == 1 || option == 2) 
        {
          if (option == 1)
          {
            enoughPoints = true;
            do{
              System.out.print ("Please enter a consonant  (" + consonantStr + ")"); 
              guess = input.next().charAt(0); 
              if ("BCDFGHJKLMNPQRSTVWXYZ".indexOf (Character.toUpperCase(guess)) >= 0)
              {
                isNotConsonant = false; 
                consonantStr = letterRemove (consonantStr, guess);
              }
              else
              {
                System.out.println (Character.toUpperCase(guess) + " is not a consonant");  
                
                isNotConsonant = true; 
              }
            } while (isNotConsonant); 
          } 
          else 
          {
            if (score >= 100)
            {
              score -= 100;
              enoughPoints = true;
              do{
                System.out.println ("Please enter a Vowel (" + vowelStr + ")"); 
                guess = input.next().charAt(0); 
                
                if ("AEIOU".indexOf (Character.toUpperCase(guess)) >= 0) 
                {
                  isNotVowel = false; 
                
                  vowelStr = letterRemove (vowelStr, guess);
                }
                else
                {
                  System.out.println (Character.toUpperCase(guess) + " is not a vowel");  
                  
                  isNotVowel = true; 
                }
              } while (isNotVowel); 
            }
            else
            {
              System.out.println ("You do not have enough points to buy a vowel..\n");
              enoughPoints = false;
            }
          } 
          if (enoughPoints == true)
          {
            
            guessCorrect = isGuessCorrect (phraseArray, guess);
            
            if (guessCorrect == true) 
            {
              score += 150;
              
              phraseArrayHidden = revealLetter (phraseArray, phraseArrayHidden, guess);
            }
            else 
            {
              System.out.println ("There are no " + guess + "'s in the phrase");
             
            }
          }
        } 
        else if (option  == 3) 
        {
          System.out.println ("Please enter what you think the hidden phrase is");
          input.nextLine();
          solveGuess = input.nextLine();
        }
        else 
        {
          System.out.println ("Please enter 1, 2, or 3.");
        } 
        
        
        
        if (option == 3)
        {
          char[] solveGuessArray = new char[solveGuess.length()];
          solveGuessArray = phraseToArray (solveGuess);
          
          playerWin = isPhraseSolved (phraseArray, solveGuessArray);
          if (playerWin == false)
          {
            System.out.println ("That is not the phrase. Your guess is incorrect");

          }
        }
        else
        {
         
          playerWin = isPhraseSolved (phraseArray, phraseArrayHidden);
        }
        if (option != 3 || option == 3 && playerWin  == false)
        {
            
           System.out.println (randomm);
            for (int index = 0; index < phraseArrayHidden.length; index++)
            {
              System.out.print (phraseArrayHidden[index] + " ");
            }
          
            System.out.println ("Score: " + score);
           
        }
      }while (option < 1 || option > 3 || playerWin == true); 
      
      if (playerWin == true) 
   
      else 
      {
       System.out.println ("\n\n\nsGame Over!");
       System.out.println(" SECRET WORD:           " +word);
      }
    
    
    
}
    

}[code]
[/code]

Is This A Good Question/Topic? 0
  • +

Replies To: I dont know why my code stops after the user enters a phrase wrong

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: I dont know why my code stops after the user enters a phrase wrong

Posted 16 May 2020 - 01:28 PM

Quote

my code stops

What does that mean?
If there is an error message, please copy the full text and paste it here.

Also post all user input to the program that caused the problem.
Was This Post Helpful? 0
  • +
  • -

#3 daberace   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-April 20

Re: I dont know why my code stops after the user enters a phrase wrong

Posted 16 May 2020 - 01:34 PM

View PostNormR, on 16 May 2020 - 01:28 PM, said:

Quote

my code stops

What does that mean?
If there is an error message, please copy the full text and paste it here.

Also post all user input to the program that caused the problem.


There is no error, the program j stops. It outputs, " the phrase is incorrect' and stops. Also, I need to know how to input "----" depending on the number of letters my word has.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: I dont know why my code stops after the user enters a phrase wrong

Posted 16 May 2020 - 01:54 PM

Please post all user input to the program that caused the problem so we can test the code with the same input that you are using.

Where is this String located: " the phrase is incorrect'
I can not find it in the program.

One problem I see is that the code uses == to compare Strings. That does not work.
You need to use the equals() method to compare Strings.

This post has been edited by NormR: 16 May 2020 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: I dont know why my code stops after the user enters a phrase wrong

Posted 26 May 2020 - 02:15 PM

while (option < 1 || option > 3 || playerWin == true){

        if (playerWin == true) {

      }else
        {
            System.out.println ("\n\n\nsGame Over!");
            System.out.println(" SECRET WORD:           " +word);
        }



    }


I think you are not using Curly Brackets correctly in your code. I just added Curly Brackets to above part of your code which was not there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1