Download Lab 5

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
ACS-1903
Lab 5
Student Number:
_____________________________
Last Name:
_____________________________
First Name:
_____________________________
In this lab you use Java facilities for reading files and searching for characters strings
 use Java classes FileReader and BufferedReader for reading a file
 use standard output (System.out.print() and prinln()) for displaying results.
You begin with the sample Java program stored at
www.uwinnipeg.ca/~rmcfadye/1903/ReadBufferedFile.java . This program was
discussed in Tuesday’s class, and it is also reprinted on page 2 of this lab. Each question
is an extension of the previous question.
You need to store www.uwinnipeg.ca/~rmcfadye/1903/lab5 on your workstation.
1. Write a Java program that prints all the lines of a file, but prefixes each line with a
line number. You will need to add one variable to the program to facilitate counting
lines. Test your program by reading www.uwinnipeg.ca/~rmcfadye/1903/lab5
When completed, show your working program to the lab demonstrator for his initials:
demonstrator's initials ___________________________________
2. Now, extend the program above to determine if a particular string of characters
appears in each line. For each line print three things: a line number, “yes” if the string
appears and “no” if it does not, and the line itself. Test your program using the file stored
at www.uwinnipeg.ca/~rmcfadye/1903/lab5 as input and by searching for the string "the".
The String class has a useful method named indexOf(). For example, to print “I found it”
when the string “the” appears in line, a programmer could write
if ( line.indexOf( “the”, 0 )
>= 0 )
System.out.println(“I found it”);
The indexOf() method returns an integer that is the position of the string “the” in line if
it is found, and -1 otherwise. The arguments “the” and 0 inform the indexOf method to
begin searching line for the string “the” at position 0 in line. This means that all of
line is searched.
Show your working program to the demonstrator.
demonstrator's initials ___________________________________
ACS-1903
Lab 5
3. Now, modify your program above to count the number of times the string “the”
appears in each line. Display a line number, number of times “the” appears, and the line
itself.
Since a line could contain the string “the” several times you need a loop to process a line.
Hence, you nest one loop inside another (nested while’s). When processing a line,
consider a nested while such as:
count=start = 0;
while ( line.indexOf( “the”, start) >= 0 )
{ count++;
start = line.indexOf( “the”, start)+3;
}
Use the same test file. Show your working program to the demonstrator.
demonstrator's initials ___________________________________
ReadBufferedFile.java:
import java.util.Scanner;
import java.io.*;
/**
* Read a file one line at a time.
*
* @author Ron
* @version 1.0
*/
public class ReadBufferedFile
{
public static void main(String[] args) throws IOException
// if the file is not available an io exception is thrown
{
// Ask user for name of file. e.g. C:/myFile.html
System.out.println("enter name of file: ");
Scanner keyboard = new Scanner(System.in);
// Obtain a buffered reader object to enable reading the file line
by line
String fileName=keyboard.next();
FileReader reader = new FileReader(fileName);
BufferedReader bReader = new BufferedReader(reader);
// Read lines of a file until end-of-file occurs
//
end-of-file inidcated by a null line
String line = bReader.readLine();
while (line != null) {
System.out.println(line);
line = bReader.readLine();
}
// File is no longer needed... so close it
bReader.close();
}
}