1 / 32

Miscellaneous topics

Miscellaneous topics. Chapter 2 Savitch. Miscellaneous topics. Standard output using System.out Input using Scanner class. The standard System class. System contains a number of interesting static methods. System.exit( 0 ); Terminates our program immediately

Download Presentation

Miscellaneous topics

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. 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/lang/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/io/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 i=12; String s = “sally”; System.out.println( i ); System.out.print( s + “ is “ + i + “ years old.” ); System.out.println();

  7. printf and rounding • What is the output of the following: class Test { public static void main ( String args[] ) { double d2 = 12.999999; System.out.printf( "%f %.2f \n\n", d2, d2 ); } }

  8. printf and rounding • What is the output of the following: class Test { public static void main ( String args[] ) { double d2 = 12.999999; System.out.printf( "%f %.2f \n\n", d2, d2 ); } } 12.999999 13.00

  9. printf() • Format string examples: d = 12.00000; System.out.printf( “%010.2f %n”, d ); d = 12.99999999; System.out.printf( “%010.2f %n”, d );

  10. 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 );

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

  12. More OO alternatives to printf() • NumberFormat • DecimalFormat

  13. Input, System.in, and the Scanner class

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

  15. Scanner class import java.util.Scanner; class Tester { public static void main ( String params[] ) { Scanner kbd = new Scanner( System.in ); … } }

  16. 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.” );

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

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

  19. Exercises on p. 91

More Related