Parsing and Formatting - PowerPoint PPT Presentation

1 / 5
About This Presentation
Title:

Parsing and Formatting

Description:

generally called the 'human-readable' or 'printable' version of the object ... the history / technology behind Java dates, times, and calendars is really twisted ... – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 6
Provided by: Steve57
Category:

less

Transcript and Presenter's Notes

Title: Parsing and Formatting


1
Parsing and Formatting
  • Obvious fact the internal representation of an
    object (a Scanner, a double, a date) is not
    easily readable by a human
  • generally called the "human-readable" or
    "printable" version of the object
  • Another obvious fact there is not a unique
    relationship between the object and the printable
    version
  • 2 versus 2 versus 002
  • 3.02 versus 03.02222 versus 30.02E-01 versus
    3.02 versus 302
  • July 4, 1776 versus 07/04/1776
  • Parsing is the process of going from a String
    (the printable version) to the Object (an int or
    double or Date)
  • Scanner already does parsing for various numeric
    types!
  • Formatting is the process of going from an object
    (int or double or Date) to a String
    representation
  • toString() and println already do formatting for
    various numeric types!

2
Parsing Numbers
  • Most of this is already done / known, because
    the Scanner already does parsing for all the
    number types (along with reading and tokenizing)
  • However, the "real Java" way to parse a number is
    ask the corresponding numeric class to do it for
    you
  • int anInteger Integer.parseInt("-3.2")
  • System.out.println(Double.parseDouble("-03.62000E0
    5"))
  • Output is -362000.0

3
Formatting Numbers
  • This is a little more complicated because
  • every valid String representation of a number
    maps into a unique number (parsing is
    unambiguous)
  • but a number does not have a unique String
    equivalent (formatting is ambiguous)
  • so formatting has to be done under the control of
    a "template" that says
  • leading plus sign?
  • leading zeros?
  • how many decimal places?
  • scientific notation?
  • also, there are some special cases of interest
    because they are convenient
  • format this number like a dollar amount
    (currency)
  • format this number like a percentage

4
Number Formatting Examples
  • General formatting
  • read an integer and print it with a leading sign
    and exactly 9 additional digits
  • read a double and print it with at most 3 decimal
    places accuracy
  • how about exactly 3 decimal places accuracy?
  • Special cases currency and percentage
  • read two wage values and print "Your new salary
    of x.xx is an increase of yyy."

5
Dates and Date Formatting
  • The motivating problem
  • Suppose an order is placed at some point in time.
  • If the order time is in the future, that's an
    error!
  • Otherwise, if the order time is before noon
    today, the order will be ready at 5PM today.
  • Otherwise, the order will be ready at noon
    tomorrow.
  • Print "Your order will be ready at Friday January
    21 at 1200 PM"
  • The starting point
  • dates represent an instant in time
  • dates are a lot like numbers you can compare
    them (is one date after another
  • arithmetic is more complicated too adding an
    hour, or a week, or subtracting a minute
  • parsing and formatting dates is much more
    complicated
  • Tuesday January 15 at 325AM PDT
  • 01/05/2004 16.42
  • the Scanner doesn't parse dates
  • the history / technology behind Java dates,
    times, and calendars is really twisted
Write a Comment
User Comments (0)
About PowerShow.com