The Scanner Class and Formatting Output
description
Transcript of The Scanner Class and Formatting Output
The Scanner Class and The Scanner Class and Formatting OutputFormatting Output
Mr. LupoliMr. Lupoli
Items you need before beginningItems you need before beginning
• From Website– Scanner Class Quick Reference Ditto
IntroductionIntroduction
• 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.)Introduction (cont.)
• Remember– Keyboard System.in– File New File ()
The Scanner’s Purpose and CreationSystem.in (Keyboard) File (File)
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();...
Scanner sc = new Scanner(new File("Lupoli.txt"));
while (sc.hasNextInt()) { int score = sc.nextInt(); }
Important InformationImportant 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 classMethods 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() (reads value)– hasNext() (checks that a value is present to read)
• This SHOULD remind you of the INTERATOR methods!!
Methods in the Scanner classMethods 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 delimiterChanging 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 delimiterChanging 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 delimiterChanging the token delimiter
Delimiter Examples
Command String returns
sc.useDelimiter(“::”); “one::two::three” “one” “two” “three”
sc.useDelimiter(“\\s+,\\s+”) “one , two , three” “one” “two” “three”
Introduction to Output FormattingIntroduction 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
PlaceholderPlaceholder
• A placeholder is used WITHIN the string of text you wish to be displayed– that place holder will display the value given a
certain formatPlaceholder Variable Type
%c char
%d int
%f double
%s String
Printf()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”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 Referenceprintf() 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!!
• Thanks all.
• Contact me if you have any questions.
SourcesSources
• 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