Reminders... • Assignment #9 is special... • No lab on Wednesday, November 24 File I/O • No class on Friday, November 26 • CS Cookie Party!!! Friday, December 10 4:30 - 7:30pm, Lake House Kitchen T- 1 T- 2 Election 2008 Writing a String to a text file FileWriter import java.io.*; • Electronic voting is lives in java.io package very much in the class WriteAFile { public static void main (String[] args) { news. FileWriter writer = new FileWriter (”votes.txt"); • After the citizens vote, the results must be writer.write( ”George W. Bush\n” ); written to a file for writer.write( ”John F. Kerry\n” ); If the file “votes.txt” does not exist, safe keeping. FileWriter will create it writer.close(); • How to do this? The write() method } takes a String } Tidy up when done T- 3 T- 4 Risky business When good programs go bad Methods in java use exceptions to tell the calling code, “Something bad happened. I failed.” The contract tells us that write() can throw an exception. T- 5 T- 6
Humoring Java An Exception is an Object* try { • The compiler needs to Throwable printStackTrace() know that YOU know getmessage() // do risky thing you’re calling a risky method. } catch (Exception ex) { • If you wrap the risky Exception code in something // try to recover called a try/catch, the // block runs only compiler will relax. // if Exception is IOException // thrown } T- 7 T- 8 *Of type Exception. Guarding against failure BufferedWriter We chain a BufferedWriter import java.io.*; import java.io.*; to FileWriter for more class WriteAFile { efficient I/O. class WriteAFile { public static void main (String[] args) { public static void main (String[] args) { try { try { FileWriter writer = new FileWriter (”votes.txt"); BufferedWriter writer = new BufferedWriter(new FileWriter (”votes.txt” )); writer.write(”George W. Bush\n”); writer.write(”George W. Bush\n”); writer.write(”John F. Kerry\n”); writer.write(”John F. Kerry\n”); writer.close(); writer.close(); } catch (IOException ex) { } catch (IOException ex) { System.out.println(“Voter error. Inform Supreme Court.”); System.out.println(“Voter error. Inform Supreme Court.”); ex.printStackTrace(); ex.printStackTrace(); } } } } } T- 9 } T- 10 The beauty of buffers Reading from a text file import java.io.*; FileReader is a connection class ReadAFile { File I/O without buffers is stream for characters public static void main (String[] args) { connecting to text file try { like shopping without a cart. BufferedReader reader = String is put into new BufferedReader(new FileReader(“votes.txt”)); When the buffer is full, a buffer with the Strings are all written to String line = null; // Variable to hold each input line other Strings while ((line = reader.readLine()) != null) { George W Bush System.out.println(line); “George W. “George W Bush” John F.Kerry “George W Bush John F Kerry” } Bush” “John F. Kerry” reader.close(); is written to is chained to Chain fileReader to } catch (Exception ex) { Buffered Reader for ex.printStackTrace(); String BufferedWriter FileWriter File more efficient reading } (chain stream that (connection stream works with characers) that writes characters } as opposed to bytes) T- 11 T- 12 }
A magic eight ball Magic8Ball import java.awt.*; • We write an “eight ball” import java.applet.*; public class Magic8Ball { program that answers yes/no public static void main (String[] args) { questions. System.out.println(StringChooser.chooseLine("answers.txt”)); • The eight ball keeps responses } like } StringChoose.chooseLine Without a doubt. reads responses from It seems unlikely. “answers.txt”, then My sources say no. randomly returns one It is certain. Concentrate and ask again. in a file named answers.txt T- 13 T- 14 An array might be nice* First we load our answers import java.io.*; import java.util.ArrayList; public class StringChooser { public static String chooseLine (String inFile) { Without It seemly My sources … a doubt unlikely say no try { BufferedReader reader = 0 1 MAXANS myArray 2 new BufferedReader(new FileReader(“votes.txt”)); private static final MAXANS = 10; // But where do we put them all? 6 String [] myArray = new String[MAXANS]; } size reader.close(); } catch (Exception ex) { ex.printStackTrace(); } return ”Ask me again tomorrow"; // Got to return something } *Why? } T- 15 T- 16 Stuffing the answers into an array Focus please! public class StringChooser { … private static final MAXANS = 10; // Max number answers allowed private static final MAXANS = 10; // Max number of answers allowed String [] myArray = new String[MAXANS]; String [] myArray = new String[MAXANS]; // Stuff into a String array … public static String chooseLine (String inFile) { String line = null; try { int size = 0; BufferedReader reader = while ((line = reader.readLine()) != null) { new BufferedReader(new FileReader(“votes.txt”)); myArray[size] = line; size++; String line = null; } int size = 0; while ((line = reader.readLine()) != null) { myArray[size] = line; size++; Without It seemly My sources … } a doubt unlikely say no reader.close(); myArray } catch (Exception ex) { MAXANS 0 1 2 ex.printStackTrace(); } 6 return ”Ask me again tomorrow"; } } size T- 17 T- 18
Avoiding array out of bounds Rolling the dice Returns a random … private static final MAXANS = 10; // Max number answers allowed double d where String [] myArray = new String[MAXANS]; 0.0 ≤ d < 1.0 … String line = null; int size = 0; return Math.random(); Returns a random while (((line = reader.readLine()) != null) && (size < MAXANS)){ double d where myArray[size] = line; 0.0 ≤ d < size return Math.random() * size; size++; } return (int)(Math.random() * size); Returns a random Without It seemly My sources … return myArray[(int)(Math.random() * size)]; a doubt unlikely say no integer i where myArray MAXANS 0 ≤ i < size -1 0 1 2 Returns a random 6 String from myArray size T- 19 T- 20 Putting it all together public class StringChooser { private static final MAXANS = 10; // Max number of answers allowed public static String chooseLine (String inFile) { try { BufferedReader reader = new BufferedReader(new FileReader(“votes.txt”)); String [] myArray = new String[MAXANS]; String line = null; int size = 0; while (((line = reader.readLine()) != null) && (size < MAXANS)) { myArray[size] = line; size++; } reader.close(); return myArray[(int)(Math.random() * size)]; } catch (Exception ex) { ex.printStackTrace(); } return ”Ask me again tomorrow"; } } T- 21
Recommend
More recommend