Lab08_Solutions - public static BufferedReader...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 import java.io.* import java.util.* import javax.swing.* /** Class to demo reading of files */ public class Lab08 { /** = the number of lines in a file chosen by the caller */ public static int lines() throws IOException { BufferedReader bf= getReader(null) String lin= bf.readLine() int n= 0 // invariant: n is the number of lines that precede line lin, and // lin is the next line to process (null if no more). while (lin != null) { // Process line lin n= n + 1 lin= bf.readLine() } return n } /** Obtain a file name from the user using a JFileChooser) and return a reader that is linked to it. If p is not null (it can be), start the JFileChooser at the path given by p*/
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: public static BufferedReader getReader(String p) { try { JFileChooser jd if (p == null) { jd= new JFileChooser() } else { jd= new JFileChooser(p) } jd.setDialogTitle("Choose input file") jd.showOpenDialog(null) FileReader fr= new FileReader(jd.getSelectedFile()) return new BufferedReader(fr) } catch (IOException e) { return null } } public static void PrintStars() throws IOException { Sheet1 Page 2 BufferedReader bf= getReader(null) String lin= bf.readLine() while (lin != null){ if (lin.indexOf('*')!=-1){ System.out.println(lin) } lin= bf.readLine() } } }...
View Full Document

Page1 / 2

Lab08_Solutions - public static BufferedReader...

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

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