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 ); 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 a/lang/System.html a/lang/System.html a/lang/System.html
Output and System.out
System.out Static member (not a method) Type is PrintStream See PrintStream.html See PrintStream.html PrintStream.html PrintStream.html Useful PrintStream methods: print() print() println() println() printf() printf()
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();
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 = ; double d2 = ; System.out.printf( "%f %.2f \n\n", d2, d2 ); System.out.printf( "%f %.2f \n\n", d2, d2 ); }}
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 = ; double d2 = ; System.out.printf( "%f %.2f \n\n", d2, d2 ); System.out.printf( "%f %.2f \n\n", d2, d2 ); }}
printf() Format string examples: d = ; System.out.printf( “%010.2f %n”, d ); d = ; System.out.printf( “%010.2f %n”, d );
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 );
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 );
More OO alternatives to printf() NumberFormatDecimalFormat
Input, System.in, and the Scanner class
Scanner class Note that System.in is of type InputStream (see a/io/InputStream.html). a/io/InputStream.html 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.
Scanner class import java.util.Scanner; class Tester { public static void main ( String params[] ) { Scanner kbd = new Scanner( System.in ); …}}
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.” );
Some useful Scanner class methods nextInt()nextLong()nextByte()nextShort()nextDouble()nextFloat()hasNextInt()hasNextLong()hasNextByte()hasNextShort()hasNextDouble()hasNextFloat()
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
Exercises on p. 91