copyright © 1998-2009 curt hill simple i/o input and output using the system and scanner objects
DESCRIPTION
Copyright © Curt Hill I/O and System System contains two objects that give access to the console: –in –out The in object reads things from keyboard The out object writes things to screenTRANSCRIPT
Copyright © 1998-2009 Curt Hill
Simple I/OInput and Output using
the System and Scanner Objects
Copyright © 1998-2009 Curt Hill
System Object
• The system object is available to every Java program
• No import statement is needed• It provides general and basic
services• Output is easiest so will be
considered first
Copyright © 1998-2009 Curt Hill
I/O and System• System contains two objects that
give access to the console:– in– out
• The in object reads things from keyboard
• The out object writes things to screen
Copyright © 1998-2009 Curt Hill
Simple Output• System.out.print(x);
– Takes any one standard type and writes it out– One argument only– Leaves the line open for further writing
• System.out.println(x);– Same as above but writes a newline afterward
• These methods are overloaded– There is one for each primitive type
Copyright © 1998-2009 Curt Hill
Overloaded Methods• Many languages allow only one
function or method of a given name– FORTRAN, Pascal, VB
• Java and C++ allow overloaded function names
• The compiler defines the function to call based on the signature
Copyright © 1998-2009 Curt Hill
Method Signature• The signature is the name and the
parameter types• Thus there may be two functions
or methods that have next as a name provided they have different parameter types– next with an int parameter – next with a double parameter
Copyright © 1998-2009 Curt Hill
Output Example• Suppose:
int a,b;double d,e;
• More code … and thenSystem.out.print(a);System.out.print(“ “);System.out.println(d);– This would produce an output line
similar to:3 7.2depending on the values of a and d
Copyright © 1998-2009 Curt Hill
Simple Input• Input is substantially more
complicated– Mostly because of error recovery
• The only thing that is real easy to read is a string– No possible errors
• Converting from a string to another type is more work
Copyright © 1998-2009 Curt Hill
Input Errors• Reading a value to be placed into
integer variable is fraught with danger• How may the string “abcde” be
interpreted as an integer?• Many other types have the same
problem• Standard input requires error catching
and it is too early to discuss this now• Since Java has the extensibility of C++
there are alternatives
Scanner• A class that makes console input
easier• It makes I/O easier by doing most of
the error handling itself without showing any of it to the user
• Obtaining it will be discussed in a later screen
• It may be used to make System.in easier to use
• We will also see how objects workCopyright © 1998-2009 Curt Hill
What is needed?• An import statement
– This is how libraries are accessed• A declaration
– This declares a handle• An initialization• Usage by accessing methods of
the Scanner class
Copyright © 1998-2009 Curt Hill
Import• The System class is available to
every Java program– It is very widely used
• There are many others that need an import– The import statement will be covered
in another presentation• After the package statement place
this:import java.util.Scanner;
Copyright © 1998-2009 Curt Hill
Declaration• Like any declaration in Java the
type is given first and then the variable name:Scanner input;
• The convention is that class names start with capital letter and variables with a lower case
• The variable name input may be any name that you want– Scanner is the predefined class name
Copyright © 1998-2009 Curt Hill
Initialization• Primitive types (int, float) are
initialized somewhat differently than object types like Scanner or String
• An object usually requires a new operator
• A typical initialization is:input = new Scanner(System.in);
• This could be combined in the declaration
Copyright © 1998-2009 Curt Hill
Form of new• The new operator allocates and
initializes a new object• The form is:new Classname(parms)
• This is usually in an assignment• The Classname is actually a
constructor call
Copyright © 1998-2009 Curt Hill
Copyright © 1998-2009 Curt Hill
Input• The Scanner class has a number of
input routines that have the following kind of names:nextType()
• Type is the capitalized name of the type
• They always return the type they specify and take no parameters
Copyright © 1998-2009 Curt Hill
Members for input• .nextInt() returns an int• .nextDouble() returns a double• Also .nextShort, .nextLong, .nextFl
oat and .nextByte • .next returns a string • Scanner has many more features
and is more complicated than needs to be considered here
Copyright © 1998-2009 Curt Hill
A Simple Example Program import java.util.Scanner;
. . . int a; float f; double d; System.out.println ("Enter 3 values "); Scanner inp = new Scanner(System.in); a = inp.nextInt(); f = inp.nextFloat(); d = inp.nextDouble(); System.out.print(a); System.out.print(" "); System.out.print(f); System.out.print(" "); System.out.println(d);
Copyright © 1998-2009 Curt Hill
Considering the Above• The data that is read is separated
by blanks• The results produced by the reads
could be used in other ways as well:– item = inp.nextInt() + inp.nextInt();
• Consider usage in simple program
Copyright © 1998-2009 Curt Hill
Copyright © 1998-2009 Curt Hill
Errors• What happens if the data on the
input line is not of an acceptable form– Not an integer for a nextInt
• An error with a traceback occurs
Copyright © 1998-2009 Curt Hill
Copyright © 1998-2009 Curt Hill
Flexibility
Copyright © 1998-2009 Curt Hill
• The Scanner class is flexible in parsing input
• The items may be on one line or several
• Here is the same program with the three values placed on separate lines:
Copyright © 1998-2009 Curt Hill