Dennis
Dennis

Reputation: 47

"cannot find symbol: variable input" error

Here is the code where I'm having trouble:

import java.util.*;
public class Game {
public static final int ROCK = 1;
public static final int PAPER = 2;
public static final int SCISSORS = 3;

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String name = intro();
    int numRounds = numRounds(name);
    game(name, numRounds);
}

public static String intro() {
    System.out.println("Welcome to Rock Paper Scissors. I, the computer, will be your opponent.");
    System.out.print("Please type in your name and press return: ");
    String name = input.nextLine();
    return name;
}

public static int numRounds(String name) {
    System.out.println("Welcome " + name + ".");
    System.out.println("All right " + name + ". How many games would you like to play?");
    System.out.print("Enter the number of rounds you want to play and press return: ");
    int numRounds = input.nextInt();
    return numRounds;
}

When I use the scanner to get the values for the user's name and number of rounds they'd like to play, I get the error. I just want to return these values for use in the main function.

Any help would be appreciated.

Upvotes: 0

Views: 15076

Answers (4)

Bozho
Bozho

Reputation: 597114

There is no input variable in the two methods that you want it - pass it as a methhod argument:

public static String intro(Scanner input) { .. }
public static int numRounds(String name, Scanner input) { .. }

...

String name = intro(input);
int numRounds = numRounds(name, input);

Apart from that, there is no game() method - define it.

Upvotes: 2

Owen
Owen

Reputation: 1551

input is declared inside the main() method, so it doesn't exist outside that method. You should declare it as a class variable, outside the main() method, so that intro() has access to it.

Upvotes: 0

JustinKSU
JustinKSU

Reputation: 4989

Your intro() and numRounds() methods are referencing input which is not visible to it. Try passing it in as a variable.

Upvotes: 0

Nanne
Nanne

Reputation: 64409

I'm a bit rusty, but I think this line:

String name = input.nextLine();

Tries to use the variable input, but that's not in its scope: it cannot "reach" it, so its not an initialised variable.

Either put this in that function:

 Scanner input = new Scanner(System.in);

Or make input "global" by declaring it as a for instance public.

Upvotes: 0

Related Questions