Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class."— 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 ); System.exit( 0 ); Terminates our program immediately long start = System.currentTimeMillis(); long start = System.currentTimeMillis(); Useful for timing programs System.out and System.in System.out and System.in The standard output and input streams See http://java.sun.com/j2se/1.5.0/docs/api/jav a/lang/System.html http://java.sun.com/j2se/1.5.0/docs/api/jav a/lang/System.html http://java.sun.com/j2se/1.5.0/docs/api/jav a/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 See http://java.sun.com/j2se/1.5.0/docs/api/java/io/ PrintStream.html http://java.sun.com/j2se/1.5.0/docs/api/java/io/ PrintStream.html http://java.sun.com/j2se/1.5.0/docs/api/java/io/ PrintStream.html Useful PrintStream methods: print() print() println() println() printf() 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

8

9

10

11

12

13

14

15

16

17

18

19

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

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

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

23 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 );

24 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 );

25 More OO alternatives to printf() NumberFormatDecimalFormat

26 Input, System.in, and the Scanner class

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

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

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

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

31 More useful scanner class methods nextBoolean() (and hasNextBoolean()) Response should be either true or false 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 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 Returns a string of the next characters upt to but not including the next newline character

32 Exercises on p. 91


Download ppt "Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class."

Similar presentations


Ads by Google