Miscellaneous topics - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Miscellaneous topics

Description:

println() prints its argument followed by a newline. Ex. int i=12; String s = 'sally' ... System.out.print( s ' is ' i ' years old.' ); System.out.println ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 24
Provided by: georgejgr
Category:

less

Transcript and Presenter's Notes

Title: Miscellaneous topics


1
Miscellaneous topics
  • Chapter 2 Savitch

2
Miscellaneous topics
  • Standard output using System.out
  • Input using Scanner class

3
The standard System class
  • System contains a number of interesting static
    methods.
  • System.exit( 0 )
  • Terminates our program immediately
  • long start System.currentTimeMillis()
  • Useful for timing programs
  • System.out and System.in
  • The standard output and input streams
  • See http//java.sun.com/j2se/1.5.0/docs/api/java/l
    ang/System.html

4
Output and System.out
5
System.out
  • Static member (not a method)
  • Type is PrintStream
  • See http//java.sun.com/j2se/1.5.0/docs/api/java/i
    o/PrintStream.html
  • Useful PrintStream methods
  • print()
  • println()
  • printf()

6
print() and println()
  • print() prints its argument
  • println() prints its argument followed by a
    newline
  • Ex.
  • int i12
  • String s sally
  • System.out.println( i )
  • System.out.print( s is i years old.
    )
  • System.out.println()

7
printf()
  • What about floating point numbers?
  • Run jGRASP now.
  • Try the following
  • double d 12.99134267
  • System.out.println( d )
  • d12.00000000
  • System.out.println( d )
  • What is the output?

8
printf()
  • Definition of printf
  • public PrintStream printf ( String format,
    Object... args )
  • From http//java.sun.com/j2se/1.5.0/docs/api/java/
    io/PrintStream.htmlprintf(java.lang.String,20jav
    a.lang.Object...)

9
printf()
  • Format string examples
  • d 12.00000
  • System.out.printf( .2f, d )
  • System.out.println()
  • d 12.99999999
  • System.out.printf( .2f, d )
  • System.out.println()
  • See http//java.sun.com/j2se/1.5.0/docs/api/java/u
    til/Formatter.htmlsyntax for the full definition.

10
printf()
  • Format string examples
  • d 12.00000
  • System.out.printf( 10.2f, d )
  • System.out.println()
  • d 12.99999999
  • System.out.printf( 10.2f, d )
  • System.out.println()

11
printf()
  • Format string examples
  • d 12.00000
  • System.out.printf( 10.2f n, d )
  • d 12.99999999
  • System.out.printf( 10.2f n, d )

12
printf()
  • Format string examples
  • d 12.00000
  • System.out.printf( 010.2f n, d )
  • d 12.99999999
  • System.out.printf( 010.2f n, d )

13
printf()
  • Format string examples
  • d 12.00000
  • System.out.printf( -10.2f feet.n, d )
  • d 12.99999999
  • System.out.printf( -10.2f feet.n, d )

14
printf()
  • Useful for formatting other data types as well.
  • int i 22
  • System.out.printf( "d n", i )
  • System.out.printf( "6d n", i )
  • System.out.printf( "06d n", i )
  • System.out.printf( "-6d n", i )
  • System.out.printf( "x n", i )

15
printf()
  • Useful for Strings too.
  • String s "sally"
  • System.out.printf( "s eats here.n", s )
  • System.out.printf( "12s eats here.n", s )
  • //exception thrown
  • //System.out.printf( "012s eats here.n", s )
  • System.out.printf( "-12s eats here.n", s )

16
More OO alternatives to printf()
  • NumberFormat
  • DecimalFormat

17
Input, System.in, and the Scanner class
18
Scanner class
  • Note that System.in is of type InputStream (see
    http//java.sun.com/j2se/1.5.0/docs/api/java/io/In
    putStream.html).
  • And the InputStream can only read arrays of
    bytes! How can we read ints, floats, doubles,
    etc.?
  • Use the Scanner class.

19
Scanner class
  • import java.util.Scanner
  • class Tester
  • public static void main ( String params )
  • Scanner kbd new Scanner( System.in )

20
Scanner class
  • Scanner kbd new Scanner( System.in )
  • System.out.println( Enter weight and age )
  • int weight kbd.nextInt()
  • int age kbd.nextInt()
  • System.out.println( Thanks. )

21
Some useful Scanner class methods
  • nextInt()
  • nextLong()
  • nextByte()
  • nextShort()
  • nextDouble()
  • nextFloat()
  • hasNextInt()
  • hasNextLong()
  • hasNextByte()
  • hasNextShort()
  • hasNextDouble()
  • hasNextFloat()

22
More useful scanner class methods
  • nextBoolean() (and hasNextBoolean())
  • Response should be either true or false
  • next() (and hasNext())
  • Returns a string of the next characters up to but
    not including the next whitespace character
  • nextLine() (and hasNextLine())
  • Returns a string of the next characters upt to
    but not including the next newline character

23
Exercises on p. 91
Write a Comment
User Comments (0)
About PowerShow.com