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.
Related
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 3 years ago.
I have to create a method to find the biggest number via an array.
I have tried this:
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner enter = new Scanner (System.in);
int[] tab = {10,4,23,45,28,34,89,9,16,55};
int choice = 0;
do{
System.out.println("*********Menu*********");
System.out.println("1) - The biggest number : ");
System.out.println("9) - Exit :");
System.out.print("Enter your choice please : ");
choice = enter.nextInt();
switch(choice){
case 1:
System.out.println("Option 1 :");
biggest_number(big);
break;
}
} while(choice != 9);
}
public static int biggest_number(int big){
for(int i=0;i<tab.length;i++){
if(tab[i] > big){
big = tab[i];
}
}
return big;
System.out.print("The biggest number is => " + big);
}
}
I have several error messages:
Main.java:23: error: cannot find symbol
biggest_number(big);
^
symbol: variable big
location: class Main
Main.java:34: error: cannot find symbol
for(int i=0;i<tab.length;i++){
^
symbol: variable tab
location: class Main
Main.java:35: error: cannot find symbol
if(tab[i] > big){
^
symbol: variable tab
location: class Main
Main.java:36: error: cannot find symbol
big = tab[i];
^
I don't understand my errors? I have declared a parameter which is called big.
Is it return is correct also according you?
For information: I am obliged to use a method for my learning in Java.
You need to correct/change the following things in your program to make it work as you are expecting:
Pass the array itself to the method, biggest_number(...) because the scope of the array, tab is local to public static void main(String[] args) i.e. it won't be visible to the method, biggest_number(...)
Remove System.out.print("The biggest number is => " + big); after the return big; statement as it be unreachable. The program control exits the method/function after the return statement; therefore, any code after the return statement will be treated unreachable/dead which will fail compilation.
Given below is the correct program incorporating the points mentioned above:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner enter = new Scanner(System.in);
int[] tab = { 10, 4, 23, 45, 28, 34, 89, 9, 16, 55 };
int choice = 0;
do {
System.out.println();
System.out.println("*********Menu*********");
System.out.println("1) - The biggest number : ");
System.out.println("9) - Exit :");
System.out.print("Enter your choice please : ");
choice = enter.nextInt();
switch (choice) {
case 1:
System.out.println("Option 1 :");
System.out.print("The biggest number is => " + biggest_number(tab));
break;
}
} while (choice != 9);
}
public static int biggest_number(int[] tab) {
int big=tab[0];
for (int i = 0; i < tab.length; i++) {
if (tab[i] > big) {
big = tab[i];
}
}
return big;
}
}
Output:
*********Menu*********
1) - The biggest number :
9) - Exit :
Enter your choice please : 1
Option 1 :
The biggest number is => 89
*********Menu*********
1) - The biggest number :
9) - Exit :
Enter your choice please :
The method biggest_number needs only the array as input:
static int biggest_number( int[] arr) {
if ( arr.length > 0 ) [
int big = arr[0];
for ( int i=1; i < arr.length; i++ ) {
if ( arr[i] > big ) {
big = arr[i];
}
}
return big;
}
return 0; // or throw an exception
}
As the error messages says:
You have are using the parameter big the wrong way, remove it.
you can't access the array tab from public static int biggest_number. either pass it as a parameter, or declare it as a static field.
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 new to java and facing a problem while converting a string into an integer.
my coding goes here-
import java.util.Scanner;
class Binary{
public static void main(String args[]){
Scanner takeInput = new Scanner(System.in);
System.out.println("Please Give Your Input ====> ");
String binaryInput = takeInput.nextLine();
System.out.println("Your Input Is ====> "+binaryInput);
int lenGTH=binaryInput.length();
for(int i = lenGTH ; i >= 0 ; i--){
char pOsition = binaryInput.charAt(i);
int convertIntoInteger = Int.parseInt(pOsition);
int convertIntoDouble = Double.parseDouble(convertIntoInteger);
int getPower = 0;
getPower *= Math.pow(convertIntoDouble , 2);
System.out.println("Power of " + i + " index " + getPower);
}
}
}
I am giving the screenshot for error in command prompt:enter image description here
There is no such class Int in java.
Try this:
int convertIntoInteger=Integer.parseInt(String.valueOf(p0sition));
Integer class is used to parse data in int.
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:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 6 years ago.
I am a beginner in java and trying to create a program that recieves input numbers in terminal, and will continuously ask for a new numbers until 0 is entered. After 0 has been entered I want the program to summarize all of the numbers and plus them together. But when I try to compile the program I get this error:
Heres the code:
import java.util.Scanner;
public class SumTall {
public static void main(String[] args) {
Scanner tallscanner = new Scanner(System.in);
int tall = 0;
int tall1;
System.out.println("Write a number:");
tall1 = Integer.parseInt(tallscanner.nextLine());
while(tall1 > 0) {
System.out.println("Write another number:");
tall1 = Integer.parseInt(tallscanner.nextLine());
int tall2 = tall + tall1;
}
if(tall1 == 0) {
System.out.println(tall2);
}
}
}
You declared tall2 in while block declare it outside while. it will stick to that block only in your case it belong to while block but you are trying to access that variavle tall2 out side while that's the reason you can see that error. hope it will help you.
I changed the declaration part out side.
import java.util.Scanner;
public class SumTall {
public static void main(String[] args) {
Scanner tallscanner = new Scanner(System.in);
int tall = 0;
int tall1,tall2;
System.out.println("Write a number:");
tall1 = Integer.parseInt(tallscanner.nextLine());
while(tall1 > 0) {
System.out.println("Write another number:");
tall1 = Integer.parseInt(tallscanner.nextLine());
tall2 = tall + tall1;
}
if(tall1 == 0) {
System.out.println(tall2);
}
}
}
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);