6

(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.

4
  • Just as a tip; keep your code formatting consistent and readable. It'll save a lot of headaches in future assignments and future classes. Commented Nov 22, 2013 at 5:25
  • Ah. As I said, I'm new to the site, and new to Java. My apologies. It's probably not helping that it's 1:30 in the morning. Commented Nov 22, 2013 at 5:28
  • Yeah, coding into the night is never fun (I did it constantly in college). The formatting bit is almost all for your benefit; it's an absolutely critical skill since you will have to work in a team eventually (and the other people have to be able to read your code). From a Stack Overflow perspective, it'll also help you get help much faster (we can't help you if we can't read the code :P) Commented Nov 22, 2013 at 5:32
  • Fair enough. On a side note, program done, and, as a bonus, functional. Woo! Good night Internet! Commented Nov 22, 2013 at 6:23

4 Answers 4

8

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that fixed it. Much obliged.
1

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.

Comments

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

  }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.