Java: "error: cannot find symbol" - java

(Rookie mistake, I'm sure.)
I'm a first year computer science student, and attempting to write a program for an assignment, with the code;
import java.util.Scanner;
public class Lab10Ex1 {
public static void main(String[] arg) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type a number: ");
int n = keyboard.nextInt();
calcNumFactors();
}
public static void calcNumFactors(){
System.out.print(n + 1);
}
}
But upon compiling, I get the error;
Lab10Ex1.java:10: error: cannot find symbol System.out.print(n +
1);
^
symbol: variable n
location: class Lab10Ex1
If someone could explain to me what I've done wrong, or how to fix it, I would greatly appreciate it.

The n variable was declared in the main method and thus is visible only in the main method, nowhere else, and certainly not inside of the calcNumFactors method. To solve this, give your calcNumFactors method an int parameter which would allow calling methods to pass an int, such as n into the method.
public static void calcNumFactors(int number) {
// work with number in here
}
and call it like so:
int n = keyboard.nextInt();
calcNumFactors(n);

You must declare the variable n in public static void calcNumFactors()
In your code, you have to pass the value of n as an argument to the function calcNumFactors() as Hovercraft Full Of Eels said.

import java.util.Scanner;
public class Lab10Ex1 {
private static int n;
public static void main(String[] arg) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type a number: ");
n = keyboard.nextInt();
calcNumFactors();
}
public static void calcNumFactors(){
System.out.print(n + 1);
}
}

In my case, I copied an Enum file from a Grails (.groovy) project and forgot to change the extension to .java

Related

Variable being changed for seemingly no reason in Java

So i just started learning Java yesterday so apologies if this is a bit of a mess.
Basically i am testing the function menuValid() by printing the input from the function questionTime(), my problem is regardless of what my input for the menu is menuValid() will always print '0' as my answer. However the X variable is declared and printed at the same points however x will remain the same and will not be set to '0'.
Here is my code:
import java.util.Scanner;
public class Test {
public static int menu_input;
public static int x;
static void menuError() {
System.out.println("Please pick a valid option.");
questionTime();
}
public static void questionTime() {
Scanner scan = new Scanner(System.in);
try {
int menu_input = scan.nextInt();
x = 25;
if(menu_input>4 && menu_input < 9){
menuError();
}
if(menu_input>9 || menu_input<1){
menuError();
}
else{
System.out.println(menu_input);
menuValid();
}
scan.close();
} catch(Exception e) {
menuError();
}
}
public static void menuValid() {
System.out.println(menu_input);
System.out.println(x);
}
public static void main(String[] args) {
System.out.println("P4CS Mini Applications");
System.out.println("----------------------");
System.out.println("Please Select an option:");
System.out.println("1) Keep Counting Game");
System.out.println("2) Number Conversion Tool");
System.out.println("3) Universal Product Code (UPC) Calculator");
System.out.println("4) Universal Product Code (UPC) Checker");
System.out.println("9) Quit");
System.out.println("Please enter option");
questionTime();
}
}
Here is an example of the output of the code so you can see my issue:
1) Keep Counting Game
2) Number Conversion Tool
3) Universal Product Code (UPC) Calculator
4) Universal Product Code (UPC) Checker
9) Quit
Please enter option
1
1
0
25
You re-declared menu_input inside questionTime (when you did int menu_input = scan.nextInt();), which is different from how you treated x, where you just changed its value (x = 25;). So the menu_item you're setting via the Scanner is not the menu_input class variable you're printing inside menuValid.
You will have to pass menu_input to menuValid() like so -
else{
System.out.println(menu_input);
menuValid(menu_input); // see this line
}
And change menu valid function as below -
public static void menuValid(int menu_input) {
System.out.println(menu_input);
System.out.println(x);
}

How to user input here?

Here is the below code:
import comp102x.IO;
public class CalculatorEx01 {
public static void multiply() {
// Please write your code after this line
System.out.print("Enter an integer, x: ");
int x =IO.inputInt();
System.out.print("Enter an integer, y: ");
int y = IO.inputInt();
System.out.print("Answer = "+ (x*y));
}
}
And what does these errors mean?
[ERROR] cannot find symbol, symbol: method inputInt(), location: class comp102x.IO.
[ERROR] cannot find symbol, symbol: method inputInt(), location: class comp102x.IO.
How about using Scanner?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
multiply();
}
public static void multiply() {
// Please write your code after this line
Scanner IO = new Scanner(System.in);
System.out.print("Enter an integer, x: ");
int x = IO.nextInt();
System.out.print("Enter an integer, y: ");
int y = IO.nextInt();
System.out.print("Answer = " + (x * y));
IO.close();
}
}
You are using a function (method)
inputInt()
which you haven't defined anywhere or isn't present in the import class which you have used , this what the error is trying to tell you.

Display method to display results from other methods Java

I'm trying to show the results from GCD and LCM using the display method. I try accessing the numbers object in the display method and it cannot resolve the symbol. Everything works with the code I'm just not sure how else I can access the numbers object inside the display method. Any help is greatly appreciated! Thanks
public static void main(String[] args) {
TwoNumbers numbers = getNumbers();
System.out.println(numbers.getNum1());
System.out.println(+numbers.getNum2());
GCD(numbers.getNum1(), numbers.getNum2());
System.out.println(GCD(numbers.getNum1(), numbers.getNum2()));
LCM(numbers.getNum1(), numbers.getNum2());
System.out.println(LCM(numbers.getNum1(), numbers.getNum2()));
}
public static TwoNumbers getNumbers(){
int num1;
int num2;
Scanner input = new Scanner(System.in);
System.out.println("Enter your first number: ");
num1 = input.nextInt();
System.out.println("Enter your second number");
num2 = input.nextInt();
return new TwoNumbers(num1, num2);
}
public static int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
public static long LCM(int a, int b) {
return a * (b / GCD(a, b));
}
public static void display(){
}
TwoNumbers numbers = getNumbers();
If you are trying to access numbers of main method. Then you can not do that, scope of numbers is only inside main method. You can not access it directly from other method.
Either you pass the numbers as parameter to display method or you can declare numbers as class level static variable.
public static void display(TwoNumbers numbers){
//Now you have numbers inside display
}
Moreover, you do not need to call the GCD and LCM method again from the display. You can simply pass result of both methods to display method from main.
TwoNumbers gcdNumbers = GCD(numbers.getNum1(), numbers.getNum2());
display(gcdNumbers);
TwoNumbers lcmNumbers = LCM(numbers.getNum1(), numbers.getNum2());
display(lcmNumbers);

I am getting a Cannot find symbol error that I can't resolve

I am getting this error that to me looks like I am not calling the method correctly. I have reviewed the past answers here but none have specifically addressed my problem as far as I can see. This is for a class project. I realize my math in the method is most likely not correct yet but I need to get the rest working then deal with an incorrect out put. Thanks a lot!
Here is my code:
import java.util.*;
public class PrintOutNumbersInReverse {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// Gather Number
System.out.print("Enter a number between 2 and 10 digits long ");
int num = console.nextInt();
System.out.println("your number is: " + num);
// call method
System.out.println("Your number in reverse is: " + reverse);
}
public static int reverse(int num, int rNum) {
rNum = 0;
while (num != 0) {
rNum = rNum + num % 10;
num = num / 10;
}
}
}
And My error Message:
PrintOutNumbersInReverse.java:28: error: cannot find symbol
System.out.println ("Your number in reverse is: " +reverse);
^ symbol: variable reverse location: class PrintOutNumbersInReverse 1 error
Change method implementation to:
public static int reverse (int num)
{
int rNum = 0;
...
return rNum;
}
and place, that is calling this method to:
System.out.println ("Your number in reverse is: " +reverse(num));
Then should be fine
When copy pasting this into eclipse, i noticed 2 things:
1.) your reverse() method doesn't return an int, but it should because the signature of the method says so: public static int reverse(int num, int rNum). Maybe return rNum, or whatever the logic behind it might be?
2.) second, you have not declared any reverse variable in the main method. Maybe you wanted a parameterized call of reverse()?
Also it looks like, you want in the reverse() method rNum to be an output parameter. In java you can't pass primitives by reference, so whatever you do with rNum inside the method, the changes will only be present in the scope of the method. So you might want to calculate something and actually return the results of your calculations.
You need to use reverse as a method, and not a variable. Also, you are passing in a variable that is not used: rNum. You see in reverse(int num, int rNum); right after you start, it sets your rNum to 0. So why pass a number in that will get set to zero?
I did this from my phone, but this should be working code:
import java.util.*;
public class PrintOutNumbersInReverse {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// Gather Number
System.out.print("Enter a number between 2 and 10 digits long ");
int num = console.nextInt();
System.out.println("your number is: " + num);
// call method
System.out.println("Your number in reverse is: " + reverse(num)); //<-- notice how this is a method cause it has "()"
}
public static int reverse(int num) { //<-- this has "int num" in the "()". This is a parameter.
int rNum = 0;
while (num != 0) {
rNum = rNum + num % 10;
num = num / 10;
}
}
}

actual and formal argue ment lists differ in length/ found: no arguments

I have currently just started a beginner java class and have met a problem in my code that I have no idea how to solve. The idea behind the program was to use 3 methods outside of the main method to obtain the number of employees, find out the missed dates for each employee, and average the missing dates.
import java.util.Scanner;
public class Average
{
int daysAbsent = 0;
public static void main(String[] args)
{
int employees, daysAbsent, averageDays;
noOfEmp();
daysMissed();
avgDaysAbsent();
}
public static int noOfEmp()
{
int employees;
System.out.println("How many employees do you have? ");
Scanner keyboard = new Scanner(System.in);
employees = keyboard.nextInt();
return employees;
}
public static int daysMissed(int employees)
{
int daysAbsent, i;
Scanner keyboard = new Scanner(System.in);
for(i = 1; i == employees; i++)
{
System.out.println("How many days was Employee #" + i + " absent?");
daysAbsent = keyboard.nextInt();
}
return daysAbsent;
}
public static float avgDaysAbsent(int employees, int daysAbsent)
{
float averageDays;
averageDays = (daysAbsent/employees);
System.out.println("Your employees averaged " + averageDays + " days absent");
return averageDays;
}
}
When I tried the compile the code I recieved the errors that the line containing the daysMissed(); and the line with avgDaysAbsent(); has an error where the method cannot be applied to the given types, no argument found, and the actual and formal argument lists differ in length. Any help would be appreciated thanks.
You call the method daysMissed() without any argument, but it expects an int parameter.
Its signature is :
public static int daysMissed(int employees)
so you should call it with an int parameter - daysMissed(someIntVariable)
You have the same error with avgDaysAbsent(int employees, int daysAbsent), which expects two ints but you pass it none.
I'm assuming what you meant to do is :
public static void main(String[] args)
{
int employees, daysAbsent, averageDays;
employees = noOfEmp();
daysAbsent= daysMissed(employees);
averageDays = avgDaysAbsent(employees,daysAbsent);
}
int employees, daysAbsent, averageDays;
employees = noOfEmp();
Eran's correction.
And don't you think that you'll have to add the entered values before you compute an average?

Categories

Resources