0% found this document useful (0 votes)
41 views13 pages

CSE 142 Lecture Notes: Interactive Programs With Scanner

These lecture notes discuss interactive programs that can read input from the user. It introduces the Scanner class which is used to read different types of input tokens and entire lines of input from System.in. The Scanner methods like next(), nextInt(), and nextLine() are described along with examples of how the Scanner works and how it consumes input tokens.

Uploaded by

damala yahya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views13 pages

CSE 142 Lecture Notes: Interactive Programs With Scanner

These lecture notes discuss interactive programs that can read input from the user. It introduces the Scanner class which is used to read different types of input tokens and entire lines of input from System.in. The Scanner methods like next(), nextInt(), and nextLine() are described along with examples of how the Scanner works and how it consumes input tokens.

Uploaded by

damala yahya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

CSE 142 Lecture Notes

Interactive Programs with Scanner


Chapter 4

Suggested reading: 4.1 - 4.3


Suggested self-checks: Section 4.10 #1-4

These lecture notes are copyright (C) Marty Stepp 2005. May not be rehosted, copied, sold, or
modified without Marty Stepp's expressed written permission. All rights reserved.
1
Interactive programs
We have written several programs that print output to
the console.

It is also possible to read text input from the console.


The user running the program types the input into the console.
We can capture the input and use it as data in our program.

A program that processes input from the user is called


an interactive program.

Interactive programs can be challenging:


Computers and users think in very different ways.
Users tend to misbehave!
2
Input and System.in
We have now seen code that communicates with
objects.
Example objects: DrawingPanel, Graphics, Color

When we print text output to the console, we


communicate with an object named System.out .
We call the println (or print) method of the System.out
object to print a message to the console.

The object that holds the user's console input is named


System.in . But it is not easy to use...

3
Scanner
Since System.in is not easy to use by itself, we will use
a second object of a type named Scanner to help.
Once we construct the Scanner object, we can ask it to read
various kinds of input from the console.

Constructing a Scanner object to read console input:


Scanner <name> = new Scanner(System.in);

Example:
Scanner console = new Scanner(System.in);

4
Scanner as data source
Think of a Scanner like a faucet or showerhead that can
be attached to a source of 'water' (data). In our case,
the source of that data is System.in .
Like a faucet must be connected to a water source,
the Scanner must be connected to a data source
by writing (System.in) when constructing it.

The Scanner requires a file named Scanner.java that


must be saved into the same folder as your program.
Scanner.java is available on the course web page.

5
Scanner methods
Methods of Scanner that we will use in this chapter:

Method Description
next() reads and returns next token as a String
nextDouble() reads and returns next token as a double
nextInt() reads and returns next token as an int
nextLine() reads and returns next entire line of input as
a String

Each of these methods causes your program to pause


until the user has typed input and pressed Enter, then
it returns the typed value to your program.
(You may wish to review return values from last chapter.)

6
Example Scanner usage
public class ReadSomeInput {
public static void main(String[] args) {
System.out.print("How old are you? ");
int age;

Scanner console = new Scanner(System.in);


age = console.nextInt();

System.out.println("Wow, you're " + age);


System.out.println("That's quite old!");
}
}

Output (user input underlined):


How old are you? 14
Wow, you're 14
That's quite old!
7
Scanning tokens
token: A unit of user input. Tokens are separated by
whitespace (spaces, tabs, new lines).
Example: If the user types the following:
23 3.14 John Smith "Hello world"
45.2 19

The tokens in the input are the following, and can be


interpreted as the given types:
Token Type(s)
1. 23 int, double, String
2. 3.14 double, String
3. John String
4. Smith String
5. "Hello String
6. world" String
7. 45.2 double, String
8. 19 int, double, String
8
Consuming input
When the Scanner's methods are called, the Scanner
reads and returns the next input value to our program.
If the type of the token isn't compatible with the type we
requested, the program crashes.

Imagine the Scanner as having an invisible cursor that


moves through all the user input.
As the scanner reads each input value, it advances forward
through the user input until it has passed the given token.
This is called consuming the input token.

Key: code consumed unconsumed last token

double pi = console.nextDouble(); // 3.14


23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19
^

9
Consume input example
Example: If the following input from the user has been typed,
23 3.14 John Smith "Hello world"
45.2 19
The Scanner views it as a linear stream of data, like the following:
23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19\n
The Scanner positions its 'cursor' at the start of the user input:
23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19\n
^
As we call the various next methods on the Scanner, the scanner
moves forward:
int x = console.nextInt(); // 23
23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19\n
^
double pi = console.nextDouble(); // 3.14
23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19\n
^
String word = console.next(); // "John"
23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19\n
^
10
Line-based input
The Scanner's nextLine method consumes and returns
an entire line of input as a String.
The Scanner moves its cursor from its current position until it
sees a \n new line character, and returns all text that was
found.
The new line character is consumed but not returned.

Example:
23 3.14 John Smith "Hello world"
45.2 19

String line1 = console.nextLine();


23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19\n
^

String line2 = console.nextLine();


23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19\n
^ 11
Mixing line-based with tokens
It is not generally recommended to use nextLine in combination
with the other next__ methods, because confusing results occur.
23 3.14
Joe "Hello world"
45.2 19

int n = console.nextInt(); // 23
23\t3.14\nJoe\t"Hello world"\n\t\t45.2 19\n
^
double x = console.nextDouble(); // 3.14
23\t3.14\nJoe\t"Hello world"\n\t\t45.2 19\n
^

// User intends to grab the John Smith "Hello world" line


// but instead receives an empty line!
String line = console.nextLine(); // ""
23\t3.14\nJoe\t"Hello world"\n\t\t45.2 19\n
^
// Calling nextLine again will get the line we wanted.
String line2 = console.nextLine(); // "Joe\t\"Hello world\""
23\t3.14\nJoe\t"Hello world"\n\t\t45.2 19\n
^
12
Line-and-token example
Here's another example of the confusing behavior:
Scanner console = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = console.nextInt();

System.out.print("Now enter your name: ");


String name = console.nextLine();

System.out.println(name + " is " + age + " years old.");

Log of execution (user input underlined):


Enter your age: 12
Now enter your name: Marty Stepp
is 12 years old.
Why?
User's overall input: 12\nMarty Stepp
After nextInt(): 12\nMarty Stepp
^
After nextLine(): 12\nMarty Stepp
^ 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy