Here is the Assignment:
1. Display a row of dashes to rpresent the word.
2. Prompt the user for a letter guess.
3. If the letter guessed is part of the word, then display that letter in place of the corresponding dash.
4. Repeat steps 2 and 3 until all the letters have been guessed or the user has entered an exclaimation point.
5. If an exclamation point has been entered, prompt the user to guess the entire word.
6. If the player correctly guesses the entire word or all the letters have been guessed, then display a message indicating that the player has won, otherwise the message should indicate that the player has lost.
7. Display the secret word and the number of guesses.
I'm trying to figure out how to fix these errors, and how I can solve this program.
Here are my errors:
J:\AP Computer Science\WordGuess.java:25: error: cannot find symbol
str = in.nextString ();
^
symbol: method nextString()
location: variable in of type Scanner
J:\AP Computer Science\WordGuess.java:28: error: method equalsIgnoreCase in class String cannot be applied to given types;
if (str.equalsIgnoreCase ('B'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:36: error: method equalsIgnoreCase in class String cannot be applied to given types;
else if (str.equalsIgnoreCase ('R'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:43: error: method equalsIgnoreCase in class String cannot be applied to given types;
else if (str.equalsIgnoreCase ('A'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:50: error: method equalsIgnoreCase in class String cannot be applied to given types;
else if (str.equalsIgnoreCase ('I'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:57: error: method equalsIgnoreCase in class String cannot be applied to given types;
else if (str.equalsIgnoreCase ('N'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:68: error: cannot find symbol
guess = in.nextString ();
^
symbol: method nextString()
location: variable in of type Scanner
7 errors
Tool completed with exit code 1
1. Display a row of dashes to rpresent the word.
2. Prompt the user for a letter guess.
3. If the letter guessed is part of the word, then display that letter in place of the corresponding dash.
4. Repeat steps 2 and 3 until all the letters have been guessed or the user has entered an exclaimation point.
5. If an exclamation point has been entered, prompt the user to guess the entire word.
6. If the player correctly guesses the entire word or all the letters have been guessed, then display a message indicating that the player has won, otherwise the message should indicate that the player has lost.
7. Display the secret word and the number of guesses.
//Imports the Scanner class
import java.util.Scanner;
public class WordGuess
{
public static void main (String [] args)
{
//Initializes scanner for user input
Scanner in = new Scanner (System.in);
String Brain = "Brain";
String Word = "-----";
String str = " ";
String guess = " ";
boolean win = false;
int numOfGuesses = 0;
//Displays a row of dashes that represents the word
System.out.println(Word);
while (str != "!")
{
//Prompts the user for a letter guess
System.out.println ("Enter in your letter guess (! To guess the entire word): ");
str = in.nextString ();
//If the letter guessed is part of the word, then display that letter in place of the corresponding dash.
if (str.equalsIgnoreCase ('B'))
{
String sub = Brain.substring (0,1);
sub = Word.substring (0,1);
System.out.println (sub);
numOfGuesses = numOfGuesses + 1;
}
else if (str.equalsIgnoreCase ('R'))
{
String sub2 = Brain.substring (0, 2);
sub2 = Word.substring (0, 2);
System.out.println (sub2);
numOfGuesses = numOfGuesses + 1;
}
else if (str.equalsIgnoreCase ('A'))
{
String sub3 = Brain.substring (0,3);
sub3 = Word.substring (0,3);
System.out.println (sub3);
numOfGuesses = numOfGuesses + 1;
}
else if (str.equalsIgnoreCase ('I'))
{
String sub4 = Brain.substring (0,4);
sub4 = Word.substring (0,4);
System.out.println (sub4);
numOfGuesses = numOfGuesses + 1;
}
else if (str.equalsIgnoreCase ('N'))
{
String sub5 = Brain.substring (0,5);
sub5 = Word.substring (0,5);
System.out.println (sub5);
numOfGuesses = numOfGuesses + 1;
}
//If an exclamation point has been entered, prompt the user to guess the entire word.
else if (str.equals('!'))
{
System.out.println ("What is your guess?:");
guess = in.nextString ();
}
else
System.out.println ("That is an incorrect guess");
numOfGuesses = numOfGuesses + 1;
//If the user guesses the word correctly, then a win message is displayed.
if (guess = Brain)
{
win = true;
System.out.println ("You guessed the word Brain! You win!");
System.out.println (Brain);
}
else if (win = false)
{
System.out.println ("Nice try, but you guessed the word incorrectly. You lose");
System.out.println (Brain);
}
}//Ends while loop
}//Ends the main method
}//Ends the class
I'm trying to figure out how to fix these errors, and how I can solve this program.
Here are my errors:
J:\AP Computer Science\WordGuess.java:25: error: cannot find symbol
str = in.nextString ();
^
symbol: method nextString()
location: variable in of type Scanner
J:\AP Computer Science\WordGuess.java:28: error: method equalsIgnoreCase in class String cannot be applied to given types;
if (str.equalsIgnoreCase ('B'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:36: error: method equalsIgnoreCase in class String cannot be applied to given types;
else if (str.equalsIgnoreCase ('R'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:43: error: method equalsIgnoreCase in class String cannot be applied to given types;
else if (str.equalsIgnoreCase ('A'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:50: error: method equalsIgnoreCase in class String cannot be applied to given types;
else if (str.equalsIgnoreCase ('I'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:57: error: method equalsIgnoreCase in class String cannot be applied to given types;
else if (str.equalsIgnoreCase ('N'))
^
required: String
found: char
reason: actual argument char cannot be converted to String by method invocation conversion
J:\AP Computer Science\WordGuess.java:68: error: cannot find symbol
guess = in.nextString ();
^
symbol: method nextString()
location: variable in of type Scanner
7 errors
Tool completed with exit code 1