Types
Overview
- What is Scanner
- How to create scanner
- How to read input from scanner
- How to not read input from scanner
- How to read integer from Scanner
- How to read Double from Scanner
- How to read any Object-Type using Scanner
What is Scanner
Scanneris a built-in java type that reads and returns input from a specifiedInputStream.- Often,
Scanneris used with anInputStreamofSystem.into allow us to read and return input directly typed from the System.
How to create Scanner
public class MainApplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
}
}
How to read input from scanner
public class MainApplication {
public static void main(String[] args) {
OutputStream output = System.out;
InputStream input = System.in;
Scanner scanner = new Scanner(input);
output.println("What is your first name?");
String firstName = scanner.nextLine();
output.println("What is your last name?");
String lastName = scanner.nextLine();
output.println("What is your email?");
String name = scanner.nextLine();
output.println("What is your age?");
String age = scanner.nextLine();
}
}
How to not read input from scanner
- If the following information confuses you, just know this:
Scanneris only useful for its.nextLine()method.- Aside from
scanner.nextLine(), the methods defined withinScannerhave some counter-intuitive behaviors. - ↑ this means you should not use other
Scannermethods unless you are positive about the appropriateness of their use.
- Aside from
scanner.next()
- the
scanner.next()method does not return the input from the nextline.- this method is tempting to use because intuitively, one would assume their “next input” would be whatever comes after pressing the
Enterkey. - Instead,
scanner.next()will return any new input after it’s current cursor-position on the terminal. - ↑ this means a user can press the
Enterkey, butscanner.next()will not read the input on next line until it’s cursor-position has reached that point, rather than the terminal’s cursor-position. - How do synchronously move the cursor using
.next()? Who cares! Don’t use it.
- this method is tempting to use because intuitively, one would assume their “next input” would be whatever comes after pressing the
scanner.nextInt()
- the
scanner.nextInt()method does not return the input from the nextline as anint.- this method is tempting to use because intuitively, one would assume their “next
int” would be whateverintcomes after pressing theEnterkey. - Instead,
scanner.nextInt()will attempt to return new input after it’s current cursor-position on the terminal as anint. - How do synchronously move the cursor using
.next()? Who cares! Don’t use it.
- this method is tempting to use because intuitively, one would assume their “next
scanner.nextDouble()
- the
scanner.nextDouble()method does not return the input from the nextline as anDouble.- this method is tempting to use because intuitively, one would assume their “next
Double” would be whateverDoublecomes after pressing theEnterkey. - Instead,
scanner.nextDouble()will attempt to return new input after it’s current cursor-position on the terminal as anDouble. - How do synchronously move the cursor using
.nextDouble()? Who cares! Don’t use it.
- this method is tempting to use because intuitively, one would assume their “next
Refresher: How to read input from scanner
public class MainApplication {
public static void main(String[] args) {
OutputStream output = System.out;
InputStream input = System.in;
Scanner scanner = new Scanner(input);
output.println("What is your first name?");
String firstName = scanner.nextLine();
output.println("What is your last name?");
String lastName = scanner.nextLine();
output.println("What is your email?");
String name = scanner.nextLine();
output.println("What is your age?");
String age = scanner.nextLine();
}
}
How to read int from scanner
public class MainApplication {
public static void main(String[] args) {
OutputStream output = System.out;
InputStream input = System.in;
Scanner scanner = new Scanner(input);
output.println("What is your age?");
String ageAsString = scanner.nextLine();
int age = Integer.parseInt(ageAsString);
output.println("How many siblings do you have?");
String siblingsAsString = scanner.nextLine();
int siblings = Integer.parseInt(siblingsAsString);
output.println("How many cousins do you have?")
String cousinsAsString = scanner.nextLine();
int cousins = Integer.parseInt(cousinsAsString);
output.println("How many uncles do you have?")
String unclesAsString = scanner.nextLine();
int uncles = Integer.parseInt(unclesAsString);
output.println("How many aunts do you have?")
String auntsAsString = scanner.nextLine();
int aunts = Integer.parseInt(auntsAsString);
}
}
How to read double from scanner
public class MainApplication {
public static void main(String[] args) {
OutputStream output = System.out;
InputStream input = System.in;
Scanner scanner = new Scanner(input);
output.println("What is your age?");
String ageAsString = scanner.nextLine();
double age = Double.parseDouble(ageAsString);
output.println("What is 1/2 as a decimal?");
String halfAsString = scanner.nextLine();
double half = Double.parseDouble(halfAsString);
output.println("What is 1/3 as a decimal?");
String thirdAsString = scanner.nextLine();
double third = Double.parseDouble(thirdAsString);
output.println("What is 1/4 as a decimal?");
String fourthAsString = scanner.nextLine();
double fourth = Double.parseDouble(fourthAsString);
}
}
How to simplify Scanner use
- The following
IOConsoleclass couplesScanner,InputStream, andOutputStreamto allow us to more easily express the aforementioned codeblocks.
public class IOConsole {
private Scanner scanner;
public IOConsole() {
this.scanner = new Scanner(System.in);
}
public String getStringInput(String prompt) {
System.out.println(prompt);
String userInput = scanner.nextLine();
return userInput;
}
public Double getDoubleInput(String prompt) {
String userInputAsString = getStringInput(prompt);
Double userInput = Double.parseDouble(userInputAsString);
return userInput;
}
public Integer getIntegerInput(String prompt) {
return getDoubleInput(prompt).intValue();
}
}
Refresher: How to read String from IOConsole
public class MainApplication {
public static void main(String[] args) {
IOConsole console = new IOConsole();
String firstName = console.getStringInput("What is your first name?");
String lastName = console.getStringInput("What is your last name?");
String email = console.getStringInput("What is your email?");
}
}
How to read int from IOConsole
public class MainApplication {
public static void main(String[] args) {
IOConsole console = new IOConsole();
int age = console.getIntegerInput("What is your age?");
int siblings = console.getIntegerInput("How many siblings do you have?");
int cousins = console.getIntegerInput("How many cousins do you have?")
int uncles = console.getIntegerInput("How many uncles do you have?")
int aunts = console.getIntegerInput("How many aunts do you have?")
}
}
How to read double from IOConsole
public class MainApplication {
public static void main(String[] args) {
IOConsole console = new IOConsole();
double age = console.getDoubleInput("What is your age?");
double half = console.getDoubleInput("What is 1/2 as a decimal?");
double third = console.getDoubleInput("What is 1/3 as a decimal?");
double fourth = console.getDoubleInput("What is 1/4 as a decimal?");
}
}