32
Miscellaneous Miscellaneous topics topics Chapter 2 Savitch Chapter 2 Savitch

Miscellaneous topics

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Miscellaneous topicsMiscellaneous topics

Chapter 2 SavitchChapter 2 Savitch

Miscellaneous topicsMiscellaneous topics

Standard output using System.outStandard output using System.out

Input using Scanner classInput using Scanner class

The standard System classThe standard System class

System contains a number of interesting System contains a number of interesting static methods.static methods. System.exit( 0 );System.exit( 0 );

Terminates our program immediatelyTerminates our program immediately long start = System.currentTimeMillis();long start = System.currentTimeMillis();

Useful for timing programsUseful for timing programs System.out and System.inSystem.out and System.in

The standard output and input streamsThe standard output and input streams

See See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html

Output and System.outOutput and System.out

System.outSystem.out

Static member (not a method)Static member (not a method)Type is PrintStreamType is PrintStream See See

http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html

Useful PrintStream methods:Useful PrintStream methods: print()print() println()println() printf()printf()

print() and println()print() and println()

print() prints its argumentprint() prints its argument

println() prints its argument followed by a println() prints its argument followed by a newlinenewline

Ex.Ex.int i=12;int i=12;

String s = “sally”;String s = “sally”;

System.out.println( i );System.out.println( i );

System.out.print( s + “ is “ + i + “ years old.” );System.out.print( s + “ is “ + i + “ years old.” );

System.out.println();System.out.println();

printf and roundingprintf and rounding

What is the output of the following:What is the output of the following:class Test {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 );

}}

}}

printf and roundingprintf and rounding

What is the output of the following:What is the output of the following:class Test {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.0012.999999 13.00

printf()printf()

Format string examples:Format string examples:d = 12.00000;d = 12.00000;

System.out.printf( “%010.2f %n”, d );System.out.printf( “%010.2f %n”, d );

d = 12.99999999;d = 12.99999999;

System.out.printf( “%010.2f %n”, d );System.out.printf( “%010.2f %n”, d );

printf()printf()

Useful for formatting other data types as Useful for formatting other data types as well.well.int i = 22;int i = 22;

System.out.printf( "%d %n", i );System.out.printf( "%d %n", i );

System.out.printf( "%6d %n", i );System.out.printf( "%6d %n", i );

System.out.printf( "%06d %n", i );System.out.printf( "%06d %n", i );

System.out.printf( "%-6d %n", i );System.out.printf( "%-6d %n", i );

System.out.printf( "%x %n", i );System.out.printf( "%x %n", i );

printf()printf()

Useful for Strings too.Useful for Strings too.String s = "sally";String s = "sally";

System.out.printf( "%s eats here.%n", s );System.out.printf( "%s eats here.%n", s );

System.out.printf( "%12s eats here.%n", s );System.out.printf( "%12s eats here.%n", s );

//exception thrown://exception thrown:

//System.out.printf( "%012s eats here.%n", s );//System.out.printf( "%012s eats here.%n", s );

System.out.printf( "%-12s eats here.%n", s );System.out.printf( "%-12s eats here.%n", s );

More OO alternatives to printf()More OO alternatives to printf()

NumberFormatNumberFormat

DecimalFormatDecimalFormat

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

Scanner classScanner class

Note that System.in is of type InputStream Note that System.in is of type InputStream (see (see http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html).).

And the InputStream can only read arrays And the InputStream can only read arrays of bytes! How can we read ints, floats, of bytes! How can we read ints, floats, doubles, etc.?doubles, etc.?

Use the Scanner class.Use the Scanner class.

Scanner classScanner class

import java.util.Scanner;import java.util.Scanner;

class Tester {class Tester {

public static void main ( String params[] ) {public static void main ( String params[] ) {

Scanner kbd = new Scanner( System.in );Scanner kbd = new Scanner( System.in );

……

}}

}}

Scanner classScanner class

Scanner kbd = new Scanner( System.in );Scanner kbd = new Scanner( System.in );

System.out.println( “Enter weight and age” );System.out.println( “Enter weight and age” );

int weight = kbd.nextInt();int weight = kbd.nextInt();

int age = kbd.nextInt();int age = kbd.nextInt();

System.out.println( “Thanks.” );System.out.println( “Thanks.” );

Some useful Scanner class Some useful Scanner class methodsmethods

nextInt()nextInt()

nextLong()nextLong()

nextByte()nextByte()

nextShort()nextShort()

nextDouble()nextDouble()

nextFloat()nextFloat()

hasNextInt()hasNextInt()

hasNextLong()hasNextLong()

hasNextByte()hasNextByte()

hasNextShort()hasNextShort()

hasNextDouble()hasNextDouble()

hasNextFloat()hasNextFloat()

More useful scanner class methodsMore useful scanner class methods

nextBoolean() (and hasNextBoolean())nextBoolean() (and hasNextBoolean()) Response should be either true or falseResponse should be either true or false

next() (and hasNext())next() (and hasNext()) Returns a string of the next characters up to Returns a string of the next characters up to

but not including the next whitespace but not including the next whitespace charactercharacter

nextLine() (and hasNextLine())nextLine() (and hasNextLine()) Returns a string of the next characters upt to Returns a string of the next characters upt to

but not including the next newline characterbut not including the next newline character

Exercises on p. 91Exercises on p. 91