lecture2 - CS116 OBJECTORIENTED PROGRAMMINGII LECTURE2...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 116 OBJECT ORIENTED  PROGRAMMING II LECTURE 2 GEORGE KOUTSOGIANNAKIS Copyright:  2010  Illinois Institute of Technology/George Koutsogiannakis 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
OOP Review In previous lectures we discussed : User defined template classes Default constructor/ non default constructor(s). Accessor mutator methods. toString method. Equals method Other methods as required by the specification. Client classes How to use a  static  instance variable. Receiving inputs from user via command line and via the  scanner object   2
Background image of page 2
OOP Review Today we want to review: Using the scanner object to read text files. Scope of variables within the class. Formatting Numbers. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Reading Text Files with Scanner  Object We need the services of the java.io package and  specifically two classes out of that package: File class IOException class Therefore we need to import those library  classes     import    java.io.File;     import     java.io.IOException; Note: Alternatively one can import all the library classes out  of package java.io by  typing:    import    java.io.*; 4
Background image of page 4
Reading Text Files with Scanner  Object Trying to open a file for reading  (or writing) can cause  problems (called exceptions)  for various reasons of which  some are: File does not exist. File name was misspelled File is corrupted. The part of the O.S. that finds files and opens them for reading and  writing does not work.  The java program we wrote has other problems. The java run time environment is corrupted Other reasons Therefore , we need to catch the exception ,if it happens,  so  that we don’t crash our program or the system.   5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Object Within the method that needs to open the file for reading  write the following code:    try { //code to open the file for reading and  //capturing the read items goes here. } catch (IOException  ioe) 
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 20

lecture2 - CS116 OBJECTORIENTED PROGRAMMINGII LECTURE2...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online