CISC 1115
Introduction to Programming Using Java
Lab #5
Files

How to Develop and Submit your Labs

Lab 5.1 — Sums of Pairs (SumsOfPairs)

The file numbers.text contains a header value (i.e., an integer specifying the number of data items in the file), followed by that number of pairs -- one pair per line. Read the numbers in as integers and print the sum of each pair — one sum per line. (As an aside, it makes no difference whether they are on the same line or not, as nextInt reads in the next integer, whether it is on the current line of the next line. I simply placed the pairs on the same line for readability.)

Sample Test Run

For example if the file numbers.text contains:

2

1 2
3 4
the program should produce he following output:
The sum of 1 and 2 is 3
The sum of 3 and 4 is 7
Several concepts are being worked on in this exercise:

Lab 5.2 — Concatenations of Pairs (ConcatsOfPairs)

The file numbers.text contains a header value (i.e., an integer specifying the number of data items in the file), followed by that number of pairs -- one pair per line. Read the numbers in as Strings, and print out the concatenation of each pair — one pair per line.

Sample Test Run

For example, if the file numbers.text contains:

2

1 2
3 4
the program should produce the following output:
The concatenation of 1 and 2 is 12
The concatenation of 3 and 4 is 34

Lab 5.3 — nextLine Demonstration (NextLineDemo)

The file mailing_list.text contains two lines of address data in the following format:
last-name first-name house-number street-name street-type city state zip
For example:
Arnow David 2900 Bedford Avenue Brooklyn NY 11210
Write a program that reads the data from the file as a full line, using nextLine, and prints the line out. Use a for loop to process the two lines.

Sample Test Run

For example, if the file mailing_list.text contains:

Arnow David 2900 Bedford Avenue Brooklyn NY 11210
Weiss Gerald 123 Anywhere Street Nowhere NW 19826
the program should produce the following output:
The line of address data: Arnow David 2900 Bedford Avenue Brooklyn NY 11210
The line of address data: Weiss Gerald 123 Anywhere Street Nowhere NW 19826

Lab 5.4 — Mailing Label (MailingLabel)

The file mailing_list.text for this lab is formatted as in Lab 5.3:
last-name first-name house-number street-name street-type city state zip
For example:
Arnow David 2900 Bedford Avenue Brooklyn NY 11210
This time, however, there is a header value preceding the lines of data.

Write a program that reads the data from the file as individual strings using next (remember, next stops reading when it encounters whitespace — a blank, tab, or newline), and prints the data out as a mailing label (as shown below).

The label should be printed using the method (which you are to write):

void printLabel(String first, String last, String houseNumber, String streetName, String streetType, String city, String state, String zip)
(Note:: Although it is usually referred to as a 'house number', that field is actually a string as it could contain characters other than digits, e.g., 103-27 or 1115B).

Sample Test Run

For example, if the file mailing_list.text contains:

2

Arnow David 2900 Bedford Avenue Brooklyn NY 11210
Weiss Gerald 123 Anywhere Street Nowhere NW 19826
the program should produce the following output:
David Arnow
2900 Bedford Avenue
Brooklyn, NY 11210

Gerald Weiss
123 Anywhere Street
Nowhere, NW 19826

Lab 5.5 — Mailing Label — Take 2 (MailingLabel) (Approval)

Redo Lab 5.4, but this time prompt for the name of the mailing list data file (which is in the same format as before), rather than hardcoding it as mailing_list.text in the program). As before, there is a header value followed by that mamy mailing label lines of the format:

Write a program that reads the data from the file as individual strings using next (remember, next stops reading when it encounters whitespace — a blank, tab, or newline), and prints the data out as a mailing label (as shown below).

The label should be printed using the method (which you are to write):

void printLabel(String first, String last, String houseNumber, String streetName, String streetType, String city, String state, String zip)
(Note:: Although it is usually referred to as a 'house number', that field is actually a string as it could contain characters other than digits, e.g., 103-27 or 1115B).
last-name first-name house-number street-name street-type city state zip
For example:
Arnow David 2900 Bedford Avenue Brooklyn NY 11210

Sample Test Run

For example, if the file first_list.text contains:

Arnow David 2900 Bedford Avenue Brooklyn NY 11210
Weiss Gerald 123 Anywhere Street Nowhere NW 19826
here is a sample execution of the program:

Enter name of data file: first_list.text
David Arnow
2900 Bedford Avenue
Brooklyn, NY 11210

Gerald Weiss
123 Anywhere Street
Nowhere, NW 19826

Lab 5.6 — Maximum of Data from Two Files — (TwoFileMax) (Approval)

The data files, data1.text and data2.text each contains a header value followed by that number of integers. Write a program that opens the two data files (so you will need to declare and create two Scanners — one for each file), and reads in one number from each file and prints out the larger of the two and which file it cam from. You can assume the header values are always the same.

Sample Test Run

For example, if the file data1.text contains:

3

12
3
45
and the file data2.text contains:
3

9
14
102
the program should produce the following output:
The larger of 12 and 9 is 12 and comes from data1.text
The larger of 3 and 14 is 14 and comes from data2.text
The larger of 45 and 102 is 102 and comes from data2.text
  • Multiple Scanners are not only for keyboard/file combinations; here it's for two data files.
  • You can also have additional Scanner; typically one per data file (plus of course, the keyboard)