The Scanner Class and Formatting Output Presentation

advertisement
The Scanner Class and
Formatting Output
Mr. Lupoli
Items you need before beginning
• From Website
– Scanner Class Quick Reference Ditto
Introduction
• The scanner class is a STANDARDIZED
class that uses different methods for
READING in values from either the
KEYBOARD or a FILE.
• Before this addition, many java
programmers created their own scanner
class to accomplish the same task
Introduction (cont.)
• Remember
– Keyboard  System.in
– File  New File ()
The Scanner’s Purpose and Creation
System.in (Keyboard)
File (File)
Scanner sc = new Scanner(System.in);
Scanner sc = new Scanner(new File("Lupoli.txt"));
int score = sc.nextInt();...
while (sc.hasNextInt()) { int score = sc.nextInt(); }
Important Information
• Must import
– Java.util.Scanner;
• Scanner will ignore white spaces BEFORE
a value, but BY DEFAULT will STOP:
– At a white space (those not reading in a line)
– At a ‘\n’ (those reading in a line)
Methods in Scanner class
• Just remember to first INDENTIFY what exactly
you wish to read in and HOW you want to use it.
– Remember a numeric value CAN be read in as a
String!!
• Methods in the class are broken down into two
categories
– next()
– hasNext()
(reads value)
(checks that a value is present to read)
• This SHOULD remind you of the INTERATOR methods!!
Methods in the Scanner class
• Scanner Class Quick Reference
– On website
• Please note that there are MATCHING
“hasNext” for each shown on the ditto.
• There are other methods in the class that I
will not cover
– Except Delimiter
Changing the token delimiter
• Can change the token delimiter to
something other than whitespace
• useDelimiter(String pattern)
– pattern can be a simple string of characters
such as “:” or “---“
or pattern can be a regular expression (regex
pattern) for advanced matching
– note, if using a character that has meaning in
regex patterns, will have to put a “\” in front of
the character
Changing the token delimiter
• Commonly used regular expressions (can
combine these with each other and text strings)
–
–
–
–
–
.
(period) match any one character
.*
match any number of characters, including none
\\s
match a single whitespace character
\\s+ match 1 or more whitespace characters
[abc] match any one character contained within the
brackets
Changing the token delimiter
Delimiter Examples
Command
String
returns
sc.useDelimiter(“::”);
“one::two::three”
“one” “two” “three”
sc.useDelimiter(“\\s+,\\s+”)
“one
“one” “two” “three”
,
two , three”
Introduction to Output Formatting
• Most of us are used to using
System.out.println to display the data
• Using “printf” can display AND format the
output
– Printf is a OLD C command used to display
– Literally the same syntax!
• A “placeholder” is used to determine the
exact output of the variable
Placeholder
• A placeholder is used WITHIN the string of text
you wish to be displayed
– that place holder will display the value given a
certain format
Placeholder
Variable Type
%c
char
%d
int
%f
double
%s
String
Printf()
• Simplest Example
int grade1 = 80;
char grade2 = ‘B’;
String grade3 = “Passing”;
System.out.printf(“Lupoli received a %d, or %c, or %s”, grade1, grade2, grade3);
// those in red are the place holders
// those in green are the actual variables
• Notice that order or variables are important!
Placeholder “flags”
• %c, %d, is NOT the full extent
• the placeholder have flags that will change
the format of the output
• \n will be needed to end the line
printf() Quick Reference
• Use the OTHER side of the Scanner
Quick Reference
• Placeholder flags
– read what each do
– NOTICE the syntax the placeholder must use
Thanks!!
• Thanks all.
• Contact me if you have any questions.
Sources
• Students
– Mike McCoy
– Jordan Clark
• Websites
– http://java.sun.com/j2se/1.5.0/docs/api/
• Books
– Problem Solving and Program Design in C
• Hanly and Koffman
Download