A few things about Scanner:
1. Notice how I declared and initialized the Scanner as a static field of the class, outside of the
main() method. You only have to call
new Scanner(System.in) once when you run your program and the best way to do that how I showed it. Doing it inside main() complicates things when you start breaking out your program into smaller methods (it's not good practice to put all your code in main()).
2. You named your Scanner variable
Gscanner. First, this name does not follow
Java conventions. The Java convention has variable and method names start with a lowercase letter. Type names (classes, interfaces, enums, and generic parameter types) start with uppercase letters. Mixing these up makes your code confusing to read. Also, the name itself should reflect the
purpose, not the implementation. Names like
input or
keyboard make your program more readable. When choosing names, try to communicate
intent/purpose, not
implementation.