This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 7 years ago.
import java.util.Scanner;
public class GuessingGameV1
{
public static void main(String [] args)
{
//
int counter = 0;
double randNum = 0.0;
randNum = Math.random();
int rand = (int)(randNum*100);
System.out.println("Please enter your guess: ");
int guess = in.nextInt();
while(guess !=rand)
{
if((guess< rand)&&(guess>=0))
{
System.out.println("Your guess is too low");
}
else if((guess> rand)&&(guess<=100))
{
System.out.println("Your guess is too high");
}
else if (guess<0)
{
System.out.println("Out of Range! Please choose a number more than 0");
}
else if (guess<100)
{
System.out.println("Out of Range! Please choose a number less than or 100");
}
counter++;
}
System.out.println("You are correct! The number was " + rand);
System.out.println("It took you " + counter + " tries to get it!");
}
}
My program is supposed to be a guessing game. Note I am very new to Java and I have re-written my program twice and it still gives me the error! What is wrong with my program?
Basically the user should input a number and the program will spit back wether it should be higher or lower, go again until the user gets it right. After they do it tells them the number and how many times it took them to do it.
Thanks!
You havent declared variable in.
Declare it :
Scanner in = new Scanner(System.in);
Related
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 4 years ago.
I am creating a version of jeopardy in Java and I just inputted the answer methods to their respective question methods and I get the "cannot find symbol" error in regards to my parameters. Can anyone help?
public static void questions400opt1 ()
{
Random rndm = new Random();
int randomQGenerator = 1 + rndm.nextInt(5);
if(randomQGenerator == 1)
{
System.out.println("");
System.out.println("QUESTION: chris pratt voices this character in the lego movie");
answer400opt1q1(total, total2, total3);
}
else if(randomQGenerator == 2)
{
System.out.println("");
System.out.println("QUESTION: anna and elsa are the main characters of this blockbuster film");
answer400opt1q2(total, total2, total3);
}
The actual answer400opt1q1 method looks like (snippet):
public static void answer400opt1q1 (int total, int total2, int total3)
{
Scanner word = new Scanner(System.in);
Scanner num = new Scanner(System.in);
System.out.print("ANSWER:");
String answer = word.nextLine();
System.out.print("player, enter your buzzing number: ");
int playerNumber = num.nextInt();
if(playerNumber == 1)
{
if(answer.equalsIgnoreCase("emmett"))
{
total =+ 400;
System.out.println("");
System.out.println("correct!");
}
else if(!(answer.equalsIgnoreCase("emmett")))
{
total =- 400;
System.out.println("");
System.out.println("incorrect answer!");
}
}
In questions400opt1() method, total, total2, and total3 are not declared variables. Declare them first before using.
I really need help with this. Im using BlueJ and it says 'might not be initialized'. How do i fix it? its correctNumber roughly line 16ish.
import java.util.Scanner;
import java.util.Random;
public class NumberGuessingGame {
public static void main(String[] args) {
Random randomNumber = new Random();
int correctNumber;
int guessTracker;
int guessLimit = 6; //the number of tries
int userInput;
Scanner in = new Scanner(System.in);
int game = 1;
boolean winTracker = false;
while (1 == game)
correctNumber = randomNumber.nextInt(1100); //computer generates a random number, max 100
userInput = 0;
guessTracker = 0;
System.out.println("Hello and welcome to this number guessing game. Please guess the number between 1 and 100 and I will help you by telling you if your guess is too high or low: ");
while (**correctNumber** != userInput && guessTracker < guessLimit){
userInput = in.nextInt();
guessTracker++;
if (userInput == correctNumber){
System.out.println("You have won the game! Your reward is a fact game: Did you know the first working camera was invented in 1816! "); //winner message, with a unlocked fact game
System.out.println("The correct number was " + correctNumber); //the correct number
System.out.println("It took a total of " + guessTracker + " guesses"); //number of guesses it took the user to guess the right number.
}
else if (userInput < correctNumber){
System.out.println("Your number is too low"); //displays that the users guess is too low
System.out.println("Please enter your next guess: "); //// user can now eneter their next guess
}
else if (userInput > correctNumber){
System.out.println("Your number is too high"); //displays that the users guess is too high
System.out.println("Please enter your next guess: "); // user can now eneter their next guess
}
if (correctNumber != userInput){
System.out.println("Sorry you have run out of guesses! The correct number was: " + correctNumber); // displays the correct number
}
}
}
}
You need to initialize correctNumber to a value.
This is not always the case, but think about this:
you call while(1 == game) which then initialized correctNumber to a random number, correctNumber = randomNumber.nextInt(1100) this would initialize correctNumber, but when the java compiler compiles your application it can't be sure that 1 == game is true. Therefore, when the compiler gets to the next loop while (**correctNumber** != userInput && guessTracker < guessLimit) your compiler sees that correctNumber has not been initialized even though it would be by the first loop.
In short, the compiler does not know whether a loop will be entered or not, therefore user3437460 is absoultely correct in saying that you need to initialize local scope variables, in this case int correctNumber = 0 will work perfectly for you.
I really need help with this. Im using BlueJ and it says 'might not be initialized'. How do i fix it?
Local scope variables need to be initialized (assigned an initial value) before use:
int correctNumber = 0
Same applies for your other variables.
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
Building a survey application, and I am running into a roadblock with this problem. I am not understanding where my issue is exactly but when I am trying to create a two option menu, it is not allowing me to compile or run it.
errors:
Compiling 2 source files to C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\build\classes
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\src\survey\Survey.java:71: error: cannot find symbol
System.out.print("Enter text for question " + (i+1) + ": ");
symbol: variable i
location: class Survey
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\src\survey\Survey.java:75: error: cannot find symbol
questions[i] = new IntegerQuestion(input.nextLine(),maxResponses);
symbol: variable i
location: class Survey
2 errors
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\nbproject\build-impl.xml:930: The following error occurred while executing this line:
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\nbproject\build-impl.xml:270: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)
Here's how I want it to be...
Choose from the following options:
S - Create a question in a string
N - Create a question in an integer
my current code:
package survey;
import java.util.Scanner;
import java.io.Serializable;
public class Survey implements Serializable
{
private String surveyName;
private Question[] questions;
private int numQuestions;
private int maxResponses;
private boolean initialized;
public Survey(String n)
{
surveyName = n;
initialized = false;
}
//initialize() sets up the numQuestions, MaxResponses, and questions for the survey
public char Questions()
{
Scanner input = new Scanner(System.in);
System.out.println("Initializing survey \"" + surveyName + "\"\n");
//add a method for password validation!?!?!? yes!!! see the bank accounts lab
System.out.print("Enter max number of responses: ");
maxResponses = input.nextInt();
System.out.print("Enter number of questions: ");
numQuestions = input.nextInt();
input.nextLine(); //have to do this to "eat" the new line character or the next input won't work correctly
System.out.println();
questions = new Question[numQuestions];
for(int i = 0; i < numQuestions;i++)
{
char choice;
//output menu options
System.out.println();
System.out.println(" S - Create String Question");
System.out.println(" N - Create Integer Question");
//loop until a valid input is entered
System.out.print("Enter choice: ");
choice = input.next().charAt(0);
//if choice is one of the options, return it. Otherwise keep looping
if(choice == 'S' || choice == 'N' )
return choice;
else
{
System.out.println("Invalid choice. Ensure a capital letter. Please re-enter.");
choice = '?';
}
(choice == '?');
return choice; //will never get here, but required to have a return statement to compile
}
System.out.print("Enter text for question " + (i+1) + ": ");
//you will also need to ask what KIND of question - right now, defaults to integer question
questions[i] = new IntegerQuestion(input.nextLine(),maxResponses);
initialized = true;
}
/*
run() gives the survey to a new survey taker, basically asks all the questions in the survey
*/
public void startSurvey()
{
if(initialized)
{
System.out.println("Welcome to the survey \"" + surveyName + "\"\n");
for(int i = 0;i < numQuestions; i ++)
{
questions[i].askQuestion();
}
System.out.println("Thank you for participating!");
}
else
{
System.out.println("Survey has not yet been setup. Please initialize first.");
}
}
/*
displayResults() displays the raw data for the survey
*/
public void Results()
{
System.out.println("Displaying data results for \"" + surveyName + "\"\n");
for(int i = 0;i < numQuestions; i ++)
{
questions[i].displayResults();
System.out.println();
}
}
/*
displayReportSummary() should run tests on your data
Examples could be: the most common response (median), the average response (mean), or display a graph of the results?
The choices are endless!
*/
public void reportSummary()
{
}
}
You're using i outside the loop. Because you declared i in the for loop, the scope of i is the loop only. It ceases to exist as soon as the loop ends.
The error messages from the compiler tell you which line of code the error is on, and exactly what the error is. It's worth learning to read these.
This question already has answers here:
How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner
(5 answers)
Closed 7 years ago.
I'm trying to figure out why my program is skipping a user input so I can come up with a solution. If I intentionally give it bad input and cause the InputMismatchException to be thrown, it skips the inputDouble = in.nextDouble(); line. Here is what the output looks like:
Please enter floating point value or -1 to stop
15.7
Please enter floating point value or -1 to stop
15.7
Please enter floating point value or -1 to stop
r
Number format is incorrect please try again
Please enter floating point value or -1 to stop
Number format is incorrect please try again
Total is: 31.4
And here is my code
import java.util.InputMismatchException;
import java.util.Scanner;
public class AddingNumbers {
public static void main(final String[] args) {
Scanner in = new Scanner(System.in);
double inputDouble = 0;
double total = 0;
int tries = 0;
boolean done = false;
while (!done) {
if (tries < 2) {
try {
System.out.println("Please enter floating point value or -1 to stop");
inputDouble = in.nextDouble();
if (inputDouble != -1) {
total = total + inputDouble;
} else {
System.out.println("Total is: " + total);
done = true;
}
} catch (InputMismatchException exception) {
System.out.println("Number format is incorrect please try again");
tries++;
}
} else {
System.out.println("Total is: " + total);
done = true;
}
}
in.close();
}
}
You could try to put a blank in.nextLine(); right below your inputDouble=in.nextDouble();
The Scanner still has invalid input in the buffer. To "clear" the buffer you need to use in.nextLine();. Use in.nextLine(); after tries++ to solve your problem.
I made this guessing game, but I found a bug in it and I don't know how to fix it. Maybe you guys can help?
the problem is, is that i will enter ONLY 9 wrong numbers, but then it will tell me I guessed 10 times. Which I didn't.
If you find any other bugs, could you tell me how to fix those too?
AND if you can tell me any tips/techniques that I could use next time that would be great.
import java.util.Scanner;
public class main {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number_to_guess = 3;
int guess;
//int number_of_guesses = 0;
Scanner input = new Scanner(System.in);
System.out.println("Hello, please enter a number between 1 and 10. You only have 10 guesses, so be be smart!!");
for(int number_of_guesses = 1; number_of_guesses<=10;){
guess = input.nextInt();
if (guess >=1 && guess <=10){//is it between 1 and 10
if(guess == number_to_guess){//is it the right number
number_of_guesses++;//increase guess number
System.out.println("Congradulations!!! You guessed the number in " + number_of_guesses + " tries");
break;//exit the loop
}
else{//if its not the right number, then try again
System.out.println("I'm sorry. Thats not it. Please try again.");
number_of_guesses++;
if (number_of_guesses == 10){
System.out.println("I'm sorry, but it appears you have guessed 10 times and didn't get the right number. The number was " + number_to_guess);
break;
}
}
}
else{//invalid number entry
System.out.println("I'm sorry, but your number that you have enter is invalid. Please make sure it is between 1 and 10");
number_of_guesses--;//the count will go up, even though the guessed an invalid number, so set it back to what it was before
}
}
}
}
You neef to move this out of scope, since 10 is still valid
if (number_of_guesses == 10){ System.out.println("I'm sorry, but it appears you have guessed 10 times and didn't get the right number. The number was " + number_to_guess); break; }