0

I am new to JAVA, and I am having trouble trying to implement this code. Can you help me out? Thanks

import java.util.Scanner;

public class Derivative {


    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);      
        System.out.println(degree());       
    }

    public int degree(){
        System.out.println("degree of function?");
        int n = keyboard.nextInt();
        return n;
    }

}    
3
  • 3
    I am having trouble is a bad description of a problem. Do you understand variable scope? Commented Jan 23, 2015 at 16:39
  • Here are the Java tutorials. Commented Jan 23, 2015 at 16:41
  • You need to understand variable scope. You've declared keyboard within the scope of your main() method, so it's not visible in the scope of your degree() method. Not only that, but you can't call an instance method (degree is non-static) from a static method without having an instance of your class handy. Commented Jan 23, 2015 at 16:42

3 Answers 3

3

You have declared your Scanner variable in the scope of your main method. The degree method has no knowledge of any variable named keyboard that exists. If you would like to read more on variable scoping (which I believe you should), you can check out the resource here. It explains in more detail the situation you are facing.

You can do 2 things

  1. The first being you can make the Scanner a class variable.
  2. The second is to pass it into the function

Class Variable:

You can declare it above your main method, for the class variable strategy like:

public static Scanner keyboard = new Scanner(System.in);

An example of the class method is:

public static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args) {

    System.out.println(degree());

}

public static int degree() {
    System.out.println("degree of function?");
    int n = keyboard.nextInt();
    return n;
}

Pass to function:

Or, you can change your method degree to take the scanner, like:

public static int degree(Scanner keyboard) {

and call it like:

degree(keyboard)

And an example of passing it into the method is:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println(degree(keyboard));

}

public static int degree(Scanner keyboard) {
    System.out.println("degree of function?");
    int n = keyboard.nextInt();
    return n;
}

Extra:

It should also be noted, that since you are calling degree from a static method (main), you should make degree a static method. Do so by changing:

public int degree() {

to:

public static int degree() {

Sign up to request clarification or add additional context in comments.

2 Comments

Actually there is no global variables in Java. In C++ yes, but not Java. It is a Class Variable.
@user3437460 You are correct, that was a misstep on my part. I have updated the wording of my answer.
0

Two ways in which you can solve this issue:

  • Either you define a static variable of Scanner and access it without changing your method degree (make it static as you are accessing it from static method i.e. main) like public static Scanner keyboard = new Scanner(System.in);.
  • Pass scanner as method argument like degree(keyboard); and accept it when defining the method like public static int degree(Scanner keyboard) {.

Comments

0

The methods does not recognize keyboard (your scanner object) because it is not in the scope of your degree() method.

How do we identify whether it is within the scope? Easy, just look at the curly braces { and }. Anything declared within the curly brackets can be seen by other within the same pair of brackets.

That being said, you can do this:

public int degree(){
    Scanner keyboard = new Scanner(System.in);      
    System.out.println("degree of function?");
    int n = keyboard.nextInt();
    return n;
}

Bring Scanner keyboard into your method can solve the problem. (Now they are within the same scope).

Alternatively, you may declare your keyboard as static.

static Scanner keyboard = new Scanner(System.in);      

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.