ENGR 131 HW 6

From Johnwiki

Jump to: navigation, search

ENGR 131 Homework #6: Files

Due at beginning of next recitation. Turn in the following:

  • Typed sheet with answers to the questions and survey below.
  • A hard copy of your final program.
  • An email to me with your program files (.java) attached.

Contents

Introduction

This assignment will familiarize you with using text files for input and output. You may use the provided FileIO class for opening files. Files are usually identified by their names as strings. So, if you have a file in your project directory named myfile.txt, then you can open it with this code:

 Scanner input = FileIO.openRead("myfile.txt");

Now you may read from this file using the same methods you already know, since the input object is a Scanner just like we use for user input.

Instructions

Your assignment is to write a search engine. Your program must be able to open a file and search through its lines for a certain phrase, then save the lines with the phrase to another file. You must find the phrase regardless of case, so "test" should match "TEST".

It must do these things:

  1. Prompt the user for the name of the file to open.
  2. Prompt the user for a phrase to search for.
  3. For every input line that contains the phrase (ignoring case), write this line to a file called "output.txt". Number these lines starting at 1.
  4. Output to the user the number of lines found.

Example

Save this text as a file named jabberwocky.txt:

"Beware the Jabberwock, my son!
  The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
  The frumious Bandersnatch!"

He took his vorpal sword in hand:
  Long time the manxome foe he sought --
So rested he by the Tumtum tree,
  And stood awhile in thought.

And, as in uffish thought he stood,
  The Jabberwock, with eyes of flame,
Came whiffling through the tulgey wood,
  And burbled as it came!

One, two! One, two! And through and through
  The vorpal blade went snicker-snack!
He left it dead, and with its head
  He went galumphing back.

"And, has thou slain the Jabberwock?
  Come to my arms, my beamish boy!
O frabjous day! Callooh! Callay!'
  He chortled in his joy.

Then run the program like so:

What file would you like to open? jabberwocky.txt
What phrase are you searching for? jabberwock
3 lines found.

Then the file output.txt will contain:

1. "Beware the Jabberwock, my son!
2.   The Jabberwock, with eyes of flame,
3. "And, has thou slain the Jabberwock?

Code

This is the code for the FileIO class. Add this to your project as a separate file to use it. Given a filename, this class will return a Scanner for input or a Formatter for output. See Scanner and Formatter in the Java documentation for help.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Formatter;

public class FileIO
{
    public static Scanner openRead(String fileName)
    {
        Scanner input = null;
        try
        {
            input = new Scanner(new File(fileName));
        }
        catch(FileNotFoundException fileNotFoundException)
        {
            System.out.println(fileName + " not found");
            System.exit(1);          
        }
        return input;
    }

    public static Formatter openWrite(String fileName)
    {
        Formatter output = null;
        try
        {
            output = new Formatter(fileName);
        }
        catch(FileNotFoundException fileNotFoundException)
        {
            System.out.println(fileName + " not found");
            System.exit(1);          
        }
        return output;
    }
}

Example

This program tests FileIO by reading numbers in the file "numbers.txt" and writing the even ones to the file "evens.txt".

import java.util.Scanner;
import java.util.Formatter;

public class FileIOTest
{
    public static void main(String args[])
    {
        Scanner input;
        Formatter output;

        // Open files.
        input = FileIO.openRead("numbers.txt");
        output = FileIO.openWrite("evens.txt");

        // Read from input and write to output.
        while (input.hasNext())
        {
            int n = input.nextInt();
            if(n % 2 == 0) // n is even
                output.format("%d\n", n);
        }

        // Close files.
        input.close();
        output.close();
    }
}

Hints

If you having a problem opening a file, make sure the file is in the same directory as your .class files. In BlueJ, this should be your project directory in Windows.

One of the things you have to do is locate a string inside of another string. Check out the String.indexOf() method.

You must also match strings regardless of case. The String.toLowerCase() method may come in handy for this.

String Java documentation

Questions

Question 1

What happens when you try to open an input file that doesn't exist? What about an output file that doesn't exist? Explain everything that happens in each case.

Question 2

If you run the program when the output file isn't empty, then what happens to the original contents of the output file?

Question 3

Does your program search for strings regardless of case? How did you do this? Is there any other way to do this?

Question 4

How might you modify your algorithm to find whole words only? For example, if you searched for "cat" then "catatonic" should not match. Please just briefly describe your modification, you don't need to give any code.

Question 5

How might you modify your algorithm to find lines containing every word of a set of words? For example, the line must contain "cat" and "dog", but not necessarily in that order.

Survey

Rate the difficulty for you on a scale of 1 to 10 (1 = Very easy for me, 10 = Very difficult for me).

Rate your understanding of the requirements on a scale of 1 to 10 (1 = Did not understand anything, 10 = Understood everything).

Personal tools