2.1. Worked Example: Create Instance of Scanner¶
Subgoals for using objects (creating instances)
Declare variable of appropriate class datatype.
Assign to variable: keyword new, followed by class name, followed by ().
Determine whether parameter(s) are appropriate (API)
Number of parameters
Data types of the parameters
You can watch this video or read through the content below it.
SG1: Declare variable of appropriate class datatype.
In the code block below, the variable name
sysinScanner was selected to better describe what we are going to do with this Scanner, which will become more clear in SG3.
SG2: Assign to variable: keyword new, followed by class name, followed by ().
sysinScanner = new Scanner();
SG3: Determine whether parameter(s) are appropriate (API)
The figure below shows the Java documentation, so we can determine what parameter(s) we need. Most of the ways to construct a Scanner require exactly 1 parameter, an object that represents the source of input data. We’ll use the standard system input from the console