Console Output in Java - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Console Output in Java

Description:

Printing values around strings. Suppose we wanted to print the values of two ... prints PI with 3 decimal places // 6 spaces total: _3.142 including the. ... – PowerPoint PPT presentation

Number of Views:159
Avg rating:3.0/5.0
Slides: 11
Provided by: luisf55
Category:
Tags: console | java | output | prints

less

Transcript and Presenter's Notes

Title: Console Output in Java


1
Console Output in Java
  • Notes Supplement
  • CS 1054 Introduction to Programming in Java
  • Spring 2008

2
print() and println()
  • So far, weve used System.out.print and
    System.out.println for output
  • With a single argument(string or numeric value)
  • println() if we require a new line at the end of
    the output
  • print() to stay on the current output line

3
Printing values around strings
  • Suppose we wanted to print the values of two
    variables, numCones (int) and unitPrice (double),
    in a neat message
  • e.g., sold 5 cones at 2.50 each.
  • Using print and println,
  • System.out.print("sold " )
  • System.out.print( numCones )
  • System.out.print( " cones at " )
  • System.out.print( unitPrice )
  • System.out.println(" each." )

4
String concatenation
  • Can use the operator on strings
  • If both arguments are strings, it is simply
    concatenated or put together, e.g.,
  • "Basket" "ball" equals "Basketball"
  • "Hi, " name equals "Hi, Bob" assuming name has
    the value "Bob"
  • If one argument is a number, that argument is
    converted to a string
  • "Hi, " 5 equals "Hi, " "5" "Hi, 5"
  • "Price " unitPrice equals "Price 2.50"
    assuming price has the value 2.50

5
Using concatenation with print and println
  • Improved version
  • System.out.print("sold " numCones )
  • System.out.println( " cones at " unitPrice "
    each." )
  • Can actually be compressed into one println
    statement
  • Note that the print or println method still has
    one argument even in these cases(a string)

6
String formatting
  • Notice that particularly with floating point
    numbers, we have no control over how many spaces
    are occupied by a number when printed
  • e.g., 5 cones sold at 2.4999998 each
  • What would be preferable is to be able to
    indicate up to how many decimal places are
    allowed
  • Can be done in Java using the printf() method

7
The printf() method
  • Even better version
  • System.out.printf("sold d cones at .2f
    each.\n",numCones, unitPrice )
  • prints 5 cones sold at 2.50 each
  • More control
  • System.out.printf("sold 3d cones at 6.2f
    each.\n",numCones, unitPrice )
  • prints __5 cones sold at __2.50 each

\n means a newline, same effect as println()
8
System.out.printf()
  • // prints PI with 3 decimal places
  • // 6 spaces total _3.142 including the .
  • public class PrintingPI
  • public static void main( String args )
  • double pi Math.PI
  • System.out.printf("pi 6.3f \n", pi)

Format specifier
9
Format specifier
  • System.out.printf("pi 6.3f \n", pi)
  • The symbol signifies the start of the specifier
  • 6 means that the width of the number is 6
    characters (including the . pad with leading
    spaces if needed)
  • The precision value 3 means that 3 decimal places
    are required
  • The conversion character f means that the
    corresponding argument is a floating-point number
  • \n results in a line break

10
Conversion options
  • 10.3f use f for float and double, first number
    is the width, second number is the number of
    decimal places
  • 5d use d for integer types, the number
    indicates the width
  • 10s use s for strings, the number indicates
    the width
  • d f s with no numbers indicated means you
    are leaving the formatting to Java defaults
Write a Comment
User Comments (0)
About PowerShow.com