1 / 17

The Scanner Class and Formatting Output

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.

bruis
Download Presentation

The Scanner Class and Formatting Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Scanner Class and Formatting Output Mr. Lupoli

  2. Items you need before beginning • From Website • Scanner Class Quick Reference Ditto

  3. 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

  4. Introduction (cont.) • Remember • Keyboard  System.in • File  New File ()

  5. 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)

  6. Methods in Scanner class • Just remember to firstINDENTIFY 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!!

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

  8. 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

  9. 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

  10. Changing the token delimiter

  11. 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

  12. 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

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

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

  15. 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

  16. Thanks!! • Thanks all. • Contact me if you have any questions.

  17. 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

More Related