-1

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.)");
    }

}
}
xPink
  • 27
  • 6

1 Answers1

1

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!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • oh wow, thanks! my teacher usually keeps it as s, is it just an unwritten java rule not to use single characters? – xPink Apr 16 '17 at 03:57
  • 1
    Its a general programming convention and not specific to Java. You must use descriptive variable names to make your code extensible and easy to read. – qrius Apr 16 '17 at 03:59
  • It is a common sense rule. You write programs so that other human beings can read them. See my updated answer, too. – GhostCat Apr 16 '17 at 04:00