Zybooks 11.6 Using Scanner in methods
How many Scanners objects are created in each code segment? public static void readWord() { Scanner scnr = new Scanner(System.in); String word = scnr.next(); // Do something with the word } public static void main(String [] args) { readWord(); readWord(); }
2 Each call to readWord() creates a different Scanner object to read the next word from the system input. The second call to readWord() may not read the expected word because the first call may have read more input than needed.
How many Scanners objects are created in each code segment? Scanner scnr1 = new Scanner(System.in); Scanner scnr2 = new Scanner(System.in); int x = scnr1.nextInt(); int y = scnr2.nextInt();
2 Each occurrence of new Scanner(System.in) creates a different Scanner. The statement scnr2.nextInt() may not assign the expected value to y because the first Scanner may have read more input than needed.
How many Scanners objects are created in each code segment? public static void readWord(Scanner scnr) { String word = scnr.next(); // Do something with the word } public static void main(String [] args) { Scanner scnr = new Scanner(System.in); readWord(scnr); readWord(scnr); }
1 Each call to readWord() uses the Scanner object created in main(), which was passed to the method, to read the next word from the system input.
How many Scanners objects are created in each code segment? Scanner scnr1 = new Scanner(System.in); int x = scnr1.nextInt(); int y = scnr1.nextInt();
1 The statement Scanner scnr1 = new Scanner(System.in); creates the only Scanner. The next statements read two integers from the keyboard with the Scanner.
Passing scanner to methods
A program should only use one Scanner per input stream. Internally, a Scanner object may read more input than needed to make subsequent reads faster. Thus, using multiple Scanners for the same input stream may lead to unexpected results If a method needs to read user input, a good practice is to create a single Scanner object in main() and pass that Scanner object to the method. The program below creates a Scanner object in main() and passes the Scanner to the getPizzaCalories() method.
Given the following input (entered on a single line) and code, sum will always be 17. Input: 8 9 True of False? public static int readInt() { Scanner scnr = new Scanner(System.in); return scnr.nextInt(); } public static void main(String [] args) { int sum = readInt() + readInt(); }
False The first call to readInt() will return 8. The second call may not always return 9 because the first Scanner may have read more input than needed.
Given the following input (entered on a single line) and code, sum will always be 3. Input: 1 2 True of False? public static int readInt(Scanner scnr) { return scnr.nextInt(); } public static void main(String [] args) { Scanner scnr = new Scanner(System.in); int sum = readInt(scnr) + readInt(scnr); }
True The program uses one Scanner to read from the System.in to avoid multiple Scanners from interfering with each other.