java beginner - cannot find symbol [duplicate] - java

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);
}
}
}

Related

Why does my while loop break after I add one object to the list [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 months ago.
I'm doing the Java MOOC fi course. I'm supposed to make a list of show names and durations, and print the ones that are shorten than certain duration.
I want the while loop to end after I enter a blank name but it ends after the first object I add to the list.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// implement here your program that uses the TelevisionProgram class
ArrayList<TelevisionProgram> programs = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Name: ");
String programName = scanner.nextLine();
if (programName.isEmpty()) {
break;
}
System.out.print("Duration: ");
int durationOfProgram = scanner.nextInt();
programs.add(new TelevisionProgram(programName, durationOfProgram));
}
System.out.println();
System.out.print("Program's maximum duration? ");
int maxDuration = scanner.nextInt();
for (TelevisionProgram show : programs) {
if (show.getDuration() <= maxDuration) {
System.out.println(show);
}
}
}
}
replaced
int durationOfProgram = scanner.nextInt();
with
int durationOfProgram = Integer.valueOf(scanner.nextLine());
and now it works but i still don't get why

Cannot Find Symbol Error When Using Parameters [duplicate]

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.

Cannot find symbol in function. [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
I am trying to get some user input in a function but for some reason my code will not recognize my input streams. When I go to compile I get an error on line 81 saying reader.readLine() can not be found. Does anyone know how to fix this? Or is it possible to have the run function happen in the initial do-while loop without any issues?s
import java.io.*;
public class JavaLab3 {
public static void main(String[] args) {
// first we define our input streams.
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
// variable declarations
String sName, playAgain;
// we catch exceptions if some are thrown.
// an exception would be entering a string when a number is expected
try {
System.out.println("what is your name?");
// we read a string from the stream
sName = reader.readLine();
do {
run();
System.out.println(sName + "would you like to play again?");
System.out.println("Please answer in lowercase 'yes' or 'no'.");
playAgain = reader.readLine();
} while (playAgain != "no");
} catch (IOException e){
System.out.println("Error reading from user");
}
}
public static int maxRun (int runTotal) {
int highScore = 0;
if (runTotal > highScore) {
highScore = runTotal;
} else {
`highScore = highScore`}
return highScore;
}
public static int run () {
Integer currentRun = 0, uNumber, counter;
final Integer MAX = 4;
final Integer MAX_NUMBER = 100;
//While current total is less than the max
while (currentRun < MAX) {
System.out.println("Please enter a number.");
//store user number
uNumber = Integer.parseInt(reader.readLine()); //Line throwing the error.
//for each number under 5 repetitions
for (counter = 0; counter <= MAX_NUMBER ; counter++ ) {
if (uNumber < 0) {
System.out.println("Please enter a positive number.");
} else if ((uNumber % 2) != 0) {
System.out.println("Please enter an even number.");
} else {
//sets current total and restarts the loop
currentRun = uNumber + currentRun;
}
}
}
//Calls maxRun function to see if score the score is the highest.
maxRun(currentRun);
System.out.println("The max for this run was, " + currentRun + ".");
return currentRun;
}
}
reader is defined within the scope of the main() method. It doesn't exist in the scope of run().
You can define it as a member of the class, so both methods will have access to it. Or pass it as a parameter between the methods.
reader is defined inside the main method and its scope is inside that, to access that in run method, you can pass that in run() method so that it can be available there.
The BufferedInput reader definition should be declared outside of the main function i.e inside the class then It will be global and accessible by any method of the class.
Class name {
buffereInput reader = ....
.....
}

How to make this code accept an input [duplicate]

This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 7 years ago.
I have been trying to find a way to make my code which does exponents accept an input for E and X. I know that if I use the scanner util, it would make this possible. But so far, I haven't found a way to do that. If you could help that would be sweet, thanks!
import java.util.Scanner;
public class SimpleExponents {
public static void main(String[] args) {
int e = 6;
int x = 4;
double result;
result = Math.pow(e,x);
System.out.println( result );
}
}
Scanner scanner = new Scanner(System.in);
int e = Integer.parseInt(scanner.next());
int x = Integer.parseInt(scanner.next());
or
Scanner scanner = new Scanner(System.in);
int e = scanner.nextInt();
int x = scanner.nextInt();

What am I doing wrong: Cannot find symbol- Varible in. [duplicate]

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);

Categories