Getting Numbers From Input Streams - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Getting Numbers From Input Streams

Description:

Getting Numbers From ... If the user types in '3.14159', what is the data type of the ... optional sign followed by a mantissa part followed by an optional ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 7
Provided by: Steve57
Category:

less

Transcript and Presenter's Notes

Title: Getting Numbers From Input Streams


1
Getting Numbers From Input Streams
  • So far we know how to hook a Scanner up to an
    input stream, and how to get an entire line of
    input into a String
  • If the user types in "3.14159", what is the data
    type of the resulting object, and what operations
    can we perform on it?
  • The important lesson there is no necessary
    relationship between
  • the int 123 and the string "123"
  • the double 3.14159 and the string "3.14159"
  • the char '1' and the int 1
  • the char '1' and the double 1.0
  • the char '1' and the String "1"
  • We need a general method for converting from
    Strings that "look like" numbers, and the
    equivalent numbers

Scanner aScanner new Scanner(System.in) String
wholeLine aScanner.nextLine()
2
Two Scanner Classes That Do Numeric Conversion
  • First, suppose the user types in "12345". Will
    either of these work? Why or why not?
  • The (correct) alternative asks the Scanner both
    to read a line of text from its input stream, and
    then to convert it to an int
  • And the obvious analogue for floating-point
    numbers is

Scanner aScanner new Scanner(System.in) String
wholeLine aScanner.nextLine() int i
(int)wholeLine
Scanner aScanner new Scanner(System.in) String
wholeLine aScanner.nextLine() int i
wholeLine
Scanner aScanner new Scanner(System.in) int i
aScanner.nextInt()
Scanner aScanner new Scanner(System.in) double
d aScanner.nextDouble()
3
Scanner Errors
  • When you scan a String, nothing can go wrong
  • because anything the user can possibly type in is
    a sequence of valid string characters
  • But that's not the case when you are scanning
    numbers
  • an integer is an optional sign followed by 1 or
    more digits
  • a double is an optional sign followed by a
    mantissa part followed by an optional decimal
    part
  • What happens when your program is expecting an
    int, but gets something else
  • a string with non-numeric characters
  • a negative sign by itself
  • a floating-point number

4
Multiple Input Tokens On A Line
  • Suppose the user types in the following line in
    response to some Scanner input request
  • " Mary Smith 20 20.25 "
  • This line has four "tokens" on it, where a token
    is defined to be a sequence of non-whitespace
    characters separated by one or more whitespace
    characters
  • There are two different sorts of Scanner methods
    that do two different things
  • aScanner.nextLine() returns the whole line,
    regardless of how many tokens
  • aScanner.next() returns the next token,
    regardless of what else is on the line
  • aScanner.nextInt() extracts the next token
    then converts it to an int
  • aScanner.nextDouble() extracts the next token
    then converts it to a double
  • If a line has just one token on it, the
    difference is not important, but if a line might
    contain multiple tokens (and you care), you have
    to be aware of the distinction between
  • one line, multiple tokens
  • multiple lines, one token per line
  • For example, what if the input specification says
    that an input line must contain first name, last
    name, hours worked, and wage rate (blank
    separated)?
  • Instead, what if the input specification says
    that the name is on the first line, then the
    hours on the second line, then the wage rate on
    the third line?

5
Formatting Numbers
  • No big issues in displaying strings on an output
    stream just print them out
  • Formatting numbers is a bigger deal, because how
    they are displayed is significant (apart from the
    numeric value). For example, consider the double
    value 1.235
  • 1.235 just itself to full precision
  • 1.2350 padded to four decimal places
  • 1.24 rounded to two decimal places
  • 1.2 rounded to one decmial place
  • 1.23 as currency
  • 124 as a percentage
  • Same value, different meanings
  • There are some nice utilities that format things
    in all those ways, but the Java syntax is a
    little strange!

6
Using the NumberFormat class
  • The NumberFormat class gives you two "formatter"
    objects that are specialized for formatting
    numbers as currency and percentages
  • NumberFormat pct NumberFormat.getPercentInstance
    ()
  • NumberFormat cur NumberFormat.getCurrencyInstanc
    e()
  • And the DecimalFormat class gives you a
    "formatter" object that can be customized to
    format a decimal number
  • DecimalFormat dec new DecimalFormat("0.00")
  • a zero means a digit must be printed there pad
    with zeros if necessary
  • a means a digit can be printed there if the
    number accuracy demands it
  • so this example will always print at least one
    number before the decimal place and at least two
    numbers after the decimal place, and maybe a
    third decimal place
  • So far, you know how to create instances of these
    objects, but what do they do?
  • In particular, how do I tell them to format the
    double number 1.235 for me?
  • The answer is simple all of them respond to a
    method format, which takes a number (double or
    int) as an argument, and produce a string
  • pct.format(1.235)
  • cur.format(1.235)
  • dec.format(1.235)
  • (But even that doesn't actually display the
    output we might still want to print the
    result.)
  • Exercise take an employee name and old salary
    from the console. Then take a new salary from
    the console. Print the employee's name, old, and
    new salary, and how much of a raise that salary
    is.
Write a Comment
User Comments (0)
About PowerShow.com