Download - Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects
![Page 1: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/1.jpg)
Copyright © 1998-2009 Curt Hill
Simple I/OInput and Output using
the System and Scanner Objects
![Page 2: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/2.jpg)
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
![Page 3: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/3.jpg)
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
![Page 4: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/4.jpg)
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
![Page 5: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/5.jpg)
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
![Page 6: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/6.jpg)
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
![Page 7: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/7.jpg)
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
![Page 8: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/8.jpg)
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
![Page 9: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/9.jpg)
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
![Page 10: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/10.jpg)
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
![Page 11: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/11.jpg)
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
![Page 12: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/12.jpg)
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
![Page 13: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/13.jpg)
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
![Page 14: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/14.jpg)
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
![Page 15: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/15.jpg)
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
![Page 16: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/16.jpg)
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
![Page 17: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/17.jpg)
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
![Page 18: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/18.jpg)
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);
![Page 19: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/19.jpg)
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
![Page 20: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/20.jpg)
Copyright © 1998-2009 Curt Hill
![Page 21: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/21.jpg)
Copyright © 1998-2009 Curt Hill
![Page 22: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/22.jpg)
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
![Page 23: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/23.jpg)
Copyright © 1998-2009 Curt Hill
![Page 24: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/24.jpg)
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:
![Page 25: Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects](https://reader035.vdocument.in/reader035/viewer/2022081605/5a4d1bc67f8b9ab0599d4b34/html5/thumbnails/25.jpg)
Copyright © 1998-2009 Curt Hill