Download Discussion07-11

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
121 Discussion #11 11/15/07
Announcements:
Ch 10 covered. Prog 5 due Friday, owl handin up, next owl due Tuesday
TA Office hours: ( LGRT 213): M 12:30 - 2:30; Tu 1-5; W 3-5; Th 10-11, 2:45-3:45; F 12:30 - 4:30
Talked about: File IO -> some on Exceptions
Brief discussion of sequential vs random-access; text vs binary files.
DisplayFile, WriteFile, show basic mechanics of reading, writing textfiles
sequentially. Echo is the big player: it isolates processLine, so any lineoriented file reading program can get a boost simply by extending Echo and
overriding processLine – as in the example shown.
Notice try/catch block in DisplayFile, WriteFile. IOExceptions different from
Arithmetic exceptions. IOExceptions are checked exceptions, must be handled.
Note: in DrJava, DisplayFile requires that you type in the full path name.
import java.util.Scanner;
import java.io.*;
public class DisplayFile{
public static void main(String[] args) throws IOException{
String fileName;
Scanner nameReader = new Scanner(System.in);
System.out.println("Enter a file name");
fileName = nameReader.nextLine();
Scanner scan = new Scanner(new FileReader(fileName));
while(scan.hasNext()){
System.out.println(scan.nextLine());
}
scan.close();
}
}
import java.util.Scanner;
import java.io.*;
public class Echo{
String fileName; // external file name
Scanner scan; // Scanner object for reads frm external file
public Echo(String f) throws IOException{
fileName = f;
scan = new Scanner(new FileReader(fileName));
}
public void readLines(){
while(scan.hasNext()){
processLine(scan.nextLine());}
scan.close();
}
public void processLine(String line){ System.out.println(line); }
}
public class ReadDriver{
public static void main(String[] args) {
try{
String fileName;
Scanner nameReader = new Scanner(System.in);
System.out.println("Enter a file name");
fileName = nameReader.nextLine();
Echo e = new Echo(fileName);
e.readLines();
}
catch(IOException e){System.out.println(e);}
}
}
import java.io.*;
public class LineCount extends Echo{
private int count = 0;
public LineCount(String f) throws IOException
{
super(f);
}
public void processLine(String line){count++;}
public int getCount(){return count;}
public void reportLineCount(){
System.out.println("Line count: " + count);
}
}
1)Given a text file, echo in all upper case, echo with odd numbered lines in
upper case, evens in lower case.
2)Given a file of ints: how many neg numbers? (use parseInt here)
3)Count # of letters in an external file
4)Print a text file backward (last line first)
import java.util.Scanner;
public class InputDriver{
public static void main(String[] args){
String s;
int value;
Scanner scan = new Scanner(System.in);
do{
try{
System.out.println("enter an integer");
s = scan.next();
value = Integer.parseInt(s);
break; // will be skipped if s isn't all digits
} // close try
catch(Exception e){
System.out.println("input not an integer");
}
}
while (true);
System.out.println("you correctly entered this integer: " + value);
}
}
Exceptions questions:
1) how could you terminate InputDriver on a bad input?
2) Could you drop try/catch, and do this with if/then/else?
3) How would you change the code so that you terminate after 3 tries?
4) What happens if the catch stmt above reads catch(NumberFormatException e) ?
5) There are 3 things you can do with exceptional behavior: 1) ignore; 2) handle (try/catch); or acknowledge
but hand-off (using throw). Compare LineCount and InputDriver on these criteria.
Finally: the Anagram problem. Questions?
Related documents