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 4the program should produce he following output:
The sum of 1 and 2 is 3 The sum of 3 and 4 is 7
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 4the program should produce the following output:
The concatenation of 1 and 2 is 12 The concatenation of 3 and 4 is 34
+
used for string concatenation or numeric addition.
nextLine
Demonstration (NextLineDemo)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 zipFor example:
Arnow David 2900 Bedford Avenue Brooklyn NY 11210Write 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 19826the 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
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 zipFor example:
Arnow David 2900 Bedford Avenue Brooklyn NY 11210This 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 19826the program should produce the following output:
David Arnow 2900 Bedford Avenue Brooklyn, NY 11210 Gerald Weiss 123 Anywhere Street Nowhere, NW 19826
nextLine
nextLine
, but it less flexible in not allowing whitespace in
the strings being read in
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 zipFor 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 19826here is a sample execution of the program:
nextLine
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 Scanner
s — 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 45and the file
data2.text
contains:
3 9 14 102the 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
Scanner
s are not only for keyboard/file combinations; here it's for two data files.
Scanner
; typically one per data file (plus of course, the keyboard)