06.morequestions

06.morequestions - Write a method that, given a...

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

View Full Document Right Arrow Icon
Write a method that, given a BufferedReader and a character, counts and returns the number of times the character occurs in the file. Make sure you catch the possible IOException in a try-catch statement. private static int countChars(BufferedReader br, char ch) { //initialize count. This MUST be done outside the try-catch. Why? int count = 0; try { // Read Line String line = inputFile.readLine(); while(line != null) { // Check line for character for(int i=0; i<line.length(); i++) { if(line.charAt(i) == ch) { count++; }//end: if() }//end: for(i) // read Next line line = inputFile.readLine(); } // Close file inputFile.close(); } catch(IOException e) { System.out.println("There was a problem reading from " + fileName); }//end: try-catch // Return number of ch in file return count; }//end: class ReadFile
Background image of page 1

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

View Full DocumentRight Arrow Icon
number of times the character occurs in the file. Make sure you catch the possible IOException in a try-catch statement.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/07/2009 for the course CSE 201 taught by Professor All during the Spring '09 term at Ohio State.

Page1 / 4

06.morequestions - Write a method that, given a...

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

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