Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects.

Similar presentations


Presentation on theme: "Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects."— Presentation transcript:

1 Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects

2 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

3 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

4 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

5 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

6 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

7 Copyright © 1998-2009 Curt Hill Output Example Suppose: int a,b; double d,e; More code … and then System.out.print(a); System.out.print(“ “); System.out.println(d); –This would produce an output line similar to: 3 7.2 depending on the values of a and d

8 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

9 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

10 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 work Copyright © 1998-2009 Curt Hill

11 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

12 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

13 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

14 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

15 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

16 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

17 Copyright © 1998-2009 Curt Hill Members for input.nextInt() returns an int.nextDouble() returns a double Also.nextShort,.nextLong,.nextFloat and.nextByte.next returns a string Scanner has many more features and is more complicated than needs to be considered here

18 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);

19 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

20 Copyright © 1998-2009 Curt Hill

21

22 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

23

24 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:

25 Copyright © 1998-2009 Curt Hill


Download ppt "Copyright © 1998-2009 Curt Hill Simple I/O Input and Output using the System and Scanner Objects."

Similar presentations


Ads by Google