320 likes | 422 Vues
This concise guide delves into Java's System class, focusing on standard output and input mechanisms. Learn to use System.out for formatted printing and discover the Scanner class for reading user input efficiently. Understand important methods like print(), println(), and printf() for displaying data, along with practical examples for various data types. The guide also covers techniques to format numbers and strings effectively, alongside tips for input handling with Scanner. Enhance your Java programming skills by mastering these essential components.
E N D
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 • 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
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()
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[] ) { double d2 = 12.999999; 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[] ) { double d2 = 12.999999; System.out.printf( "%f %.2f \n\n", d2, d2 ); } } 12.999999 13.00
printf() • Format string examples: d = 12.00000; System.out.printf( “%010.2f %n”, d ); d = 12.99999999; 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() • NumberFormat • DecimalFormat
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.
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.” );
nextInt() nextLong() nextByte() nextShort() nextDouble() nextFloat() hasNextInt() hasNextLong() hasNextByte() hasNextShort() hasNextDouble() hasNextFloat() Some useful Scanner class methods
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