+ All Categories
Home > Documents > The Scanner Class and Formatting Output

The Scanner Class and Formatting Output

Date post: 22-Jan-2016
Category:
Upload: bruis
View: 64 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
17
The Scanner Class The Scanner Class and Formatting and Formatting Output Output Mr. Lupoli Mr. Lupoli
Transcript
Page 1: The Scanner Class and Formatting Output

The Scanner Class and The Scanner Class and Formatting OutputFormatting Output

Mr. LupoliMr. Lupoli

Page 2: The Scanner Class and Formatting Output

Items you need before beginningItems you need before beginning

• From Website– Scanner Class Quick Reference Ditto

Page 3: The Scanner Class and Formatting Output

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

Page 4: The Scanner Class and Formatting Output

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(); }

Page 5: The Scanner Class and Formatting Output

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)

Page 6: The Scanner Class and Formatting Output

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!!

Page 7: The Scanner Class and Formatting Output

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

Page 8: The Scanner Class and Formatting Output

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

Page 9: The Scanner Class and Formatting Output

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

Page 10: The Scanner Class and Formatting Output

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”

Page 11: The Scanner Class and Formatting Output

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

Page 12: The Scanner Class and Formatting Output

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

Page 13: The Scanner Class and Formatting Output

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!

Page 14: The Scanner Class and Formatting Output

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

Page 15: The Scanner Class and Formatting Output

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

Page 16: The Scanner Class and Formatting Output

Thanks!!Thanks!!

• Thanks all.

• Contact me if you have any questions.

Page 17: The Scanner Class and Formatting Output

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


Recommended