java.util.Scanner
We can create a Scanner
object to read input from the console (represented by System.in
):
package org.wecancodeit.fundamentals;
import java.util.Scanner;
public class NamePromptApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.nextLine();
System.out.println("Hello, " + name);
input.close();
}
}
Let's run it!
If I type Ford Prefect, then this is what I will see in my console:
$ What is your name?
Ford Prefect
$ Hello, Ford Prefect
Let's Break It Down
Rather than a primitive like an int
or a boolean
, our Scanner
is an object. (String
s are objects, too.) Scanner
is a class representing our input
variable's type.
Scanner input = new Scanner(System.in);
Remember, primitive types are in all lowercase, while class names are CapitalizedCamelCase
:
Scanner input = new Scanner(System.in);
Creating the scanner
We use the new
operator to create an object. The other operators we've seen have used one (+
, -
, =
) or more (++
, !=
) symbols, but new
is a legit operator, too:
Scanner input = new Scanner(System.in);
It is followed by a special method called a constructor
. The job of a constructor is to construct an object. A constructor's name is the same as the name of the type of thing it's constructing:
Scanner input = new Scanner(System.in);
This constructor accepts System.in
as a parameter in order to "scan" input from the console. System.in
is the complement of System.out
, representing console input:
Scanner input = new Scanner(System.in);
More on classes, objects, and constructors later.
Dat Message Do
Think of methods as messages we send to objects. We have been using the main
method:
public static void main(String[] args) {
// your code here
}
main
is the message that the Java Virtual Machine (JVM) sends to our application's class to tell it to do something. You may hear a class containing a main
method called a "main class".
Can Ya Do Something For Me?
We call the nextLine
method when we want to ask our Scanner
for the next line of user input. This is like saying, "Hey input! Can ya give me the next line?":
String name = input.nextLine();
See that dot lurking in there? It's an operator, too. Whimsically, we refer to it as the dot operator:
String name = input.nextLine();
We use the dot operator to send messages to objects.
Ponder System.out.println("my message");
.
What's happening there?
Call Me
If we look at the Scanner class, we can find out what that nextLine()
method looks like. The part of the method before the opening curly bracket ({
) is called its signature. The signature of the nextLine()
method is:
public String nextLine()
If it wasn't public
, we wouldn't be able to see it, so we wouldn't be able to send that message:
public String nextLine()
String
indicates the method's return type. This is how it responds when we talk to it. When we call nextLine()
, its response is a String
:
public String nextLine()
Other Methods
Scanner
offers us other methods, as well. (Its capabilities are far beyond our simple needs.) We have been using nextLine()
, which reads an entire line of input. (Until you hit <Enter>.)
Used as we're using it, Scanner
breaks up input by blank spaces and line breaks. Its other methods don't read entire lines. They read up to the first space or line break.
Here are some of those you may find useful:
method | what it does | return type |
---|---|---|
nextLine() | reads the next String from the input until the line break |
String |
nextInt() | reads the next int from the input until the next space or line break |
int |
nextDouble() | reads the next double from the input until the next space or line break |
double |
next() | reads the next String from the input until the next space or line break |
String |
Demo
If we do this:
Scanner input = new Scanner(System.in);
System.out.println("Enter street address:");
int houseNumber = input.nextInt();
String street = input.next();
System.out.println("house number is " + houseNumber);
System.out.println("street is " + street);
Then we can do this:
$ Enter street address:
43 Brown St
$ house number is 43
$ street is Brown