University of Toronto - Fall 2000
Department of Computer Science

Week 8 - Use a Vector to read a list of lines

Read lines of text entered by a user, and use a Vector to print the middle line from this sequence of input lines.

import java.util.*;
import java.io.*;
class PrintMiddle {
	public static void main (String[] args) throws IOException {
		BufferedReader in = new BufferedReader(...);
	
		Vector lines = new Vector();
	
		final String STOPPER = "quit";
		System.out.println("Type lines, ending with " + STOPPER);
		String line = in.readLine();
	
		while (!line.equals(STOPPER)) {
			lines.addElement(line);
			line = in.readLine();
		}

		System.out.println("The middle line:");
		System.out.println ((String)lines.elementAt (lines.size()/2));
	}
}

Program Run

Type lines, ending with quit
This is a
simple program
that should
print the middle
line of input.
quit
The middle line:
that should