As Berry sports teams get geared up for their seasons, faculty are inundated with email excusal letters from the athletic office. Okay, it's not that bad, but for this assignment, you are going to write a Java program to help a professor who is too lazy to read through all the excusals to find the names of students who are in his classes. Even if the professor knows the names of all the students in his classes, it still takes too long to read through the lists of players in each email to find names of those students who will be missing class. So, instead, he puts all the names of his students in one file and then saves all the excusal letters to individual files. You are to write a program that will read the names of the students from the student file, and then search through all the excusal letters and tell whether the names of any of those students are in a given letter.
Here is a sample run of the program you should write.
(The italics
indicate what the user types.)
Your output should
look just like this. The format of the input files is described in more
detail below.
>java SearchExcuses Please enter name of student data file: students.txt Please enter up to 10 files to search. Enter a file name or just press <Enter> to finish. File number 1: men's basketball.txt File number 2: men's baseball.txt File number 3: women's soccer.txt File number 4: women's tennis.txt File number 5: women's volleyball.txt File number 6: men's tennis.txt File number 7: Searching: men's basketball.txt Ben Elliott Searching: men's baseball.txt Benjamin Kendrick Searching: women's soccer.txt Courtney Bond Searching: women's tennis.txt Ana Gonzalez Searching: women's volleyball.txt Amanda Stiefel Searching: men's tennis.txt Jonathan Landers |
Here is an example of a data file with student names:
students.txt
. The first line in
the file indicates the number of names that are in the rest of the file.
(Hint: you may use this by reading in that number and then creating an
array of strings of that size to store the names as you read them in using a
loop.) The
names are listed as "firstname lastname" - that is, first name, followed by a
single space, followed by the last name.
The files in which you have to search for the occurrence of student names are simply arbitrary text files, such as the samples listed below. However, notice that some coaches (no one you know) list the names of the players on the team using the format "lastname, firstname" - that is, the last name, followed by a comma and a space, followed by the first name. Therefore your program will have to search for student names in these files using either the format "firstname lastname" or "lastname, firstname".
women's volleyball.txt
women's tennis.txt
women's soccer.txt
men's tennis.txt
men's basketball.txt
men's baseball.txt
This assignment is obviously meant to have you gain experience using arrays. Therefore, you will have to use arrays somehow. Think about your algorithm before you start programming. This program can be written in a single class, but it is probably a good idea to break down the necessary steps into separate methods. Your program for this assignment should not be much longer than that for assignment 7.
Before worrying about getting multiple file name inputs from the user, it is probably easier to first write a method(s) that will just read in the student data from the "students.txt" file and then search a given email file for the names of those students. Once you have this part working, you can generalize it to handle searching multiple files and asking the user to enter the names of the files.
The indexOf
method for String
s may be useful
in writing this program.
Extend your solution so that it will optionally accept the file names as command line arguments. That is, the program will still run as shown about, but it should also be possible to specify all the file names when running the program, so then it doesn't ask for user input:
> java SearchExcuses students.txt "men's baseball.txt" "women's tennis.txt" Searching: men's baseball.txt Benjamin Kendrick Searching: women's tennis.txt Ana Gonzalez |
Hand in a printout of all the commented Java program files that are necessary to make your program run correctly, and also submit them online through VikingWeb.