javadrjava

Something can't be resolved?


Trying to test my java "skills" and make a text based game--except i can't get the user input. i already importd the scanner class, and it works well w/ integers so idk what the problem is quite frankly. whenever i try to compile it, the lines containing "String name = scanner.next();" show up with a 'Scanner cannot be resolved' error.

import java.util.Scanner;
public class CH1 {
 public static void main (String args[]) {
Scanner s= new Scanner( System.in);
int answer;
System.out.println ("You're in grave danger, but first, I must know your name. Will you tell me? ");
    answer = s.nextInt();
    if (answer == 1) {
        System.out.println ("I respect your decision, but I'll need to know your name 
                    if you turn up dead, unless you want to have a one man funeral.");
        System.out.println ("What's your name?");
       String name = scanner.next();
    }
    else if (answer == 2) {
        System.out.println("Great, now what's your name?");
          String name = scanner.next();

    }
    else {
        System.out.println(" Huh? I didn't really get that. (1 for no, 2 for yes.)");
    }

}
}

Solution

  • You named that scanner s first!

    You can't just use a different name later on!

    So simply change the scanner variable name to "scanner" and keep using that name.

    Beyond that: Single character variable names are something you almost never do (except for index values in for loops). The point is: variable names should say something about the thing they denote. "s" says nothing!