Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Scanner Class and Formatting Output Mr. Lupoli.

Similar presentations


Presentation on theme: "The Scanner Class and Formatting Output Mr. Lupoli."— Presentation transcript:

1

2 The Scanner Class and Formatting Output Mr. Lupoli

3 Items you need before beginning From Website –Scanner Class Quick Reference Ditto

4 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

5 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); int score = sc.nextInt();... Scanner sc = new Scanner(new File("Lupoli.txt")); while (sc.hasNextInt()) { int score = sc.nextInt(); }

6 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)

7 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() (reads value) –hasNext() (checks that a value is present to read) This SHOULD remind you of the INTERATOR methods!!

8 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

9 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

10 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

11 Changing the token delimiter Delimiter Examples CommandStringreturns sc.useDelimiter(“::”);“one::two::three”“one” “two” “three” sc.useDelimiter(“\\s+,\\s+”)“one, two, three”“one” “two” “three”

12 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

13 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 PlaceholderVariable Type %cchar %dint %fdouble %sString

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

15 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

16 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

17 Thanks!! Thanks all. Contact me if you have any questions.

18 Sources Students –Mike McCoy –Jordan Clark Websites –http://java.sun.com/j2se/1.5.0/docs/api/http://java.sun.com/j2se/1.5.0/docs/api/ Books –Problem Solving and Program Design in C Hanly and Koffman


Download ppt "The Scanner Class and Formatting Output Mr. Lupoli."

Similar presentations


Ads by Google