Level-up challenge · CSCD210

One program that reads two-line records with both kinds of Scanner read

You have finished the room on the leftover newline and you want one program where choosing the wrong kind of read is the difference between the right output and an empty one.

Unlocked at the end of: Arrays and file I/O, from zero, after the room the-leftover-newline. Room 12 closes the block of four rooms on how a Scanner reads. After it you can predict what every read in this program returns, which is the whole difficulty of the program.

Outcomes this shows4Inputs it is run on4Timeabout 45 minutesDifficulty2 of 3

What this shows you can do

Each line below is one outcome, written the way the course records it. A program that does the whole task shows all of them at once.

  1. Student can choose between Scanner token methods (next, nextInt, nextDouble) and line methods (nextLine) based on the file format, and predict what each call consumes from the buffer.

    In this program: Every field in this format needs a decision about which kind of read to use. Measured: reading the description with a token read ends the run with InputMismatchException on the next quantity, and reading the part name with a line read puts a second space into every item line.

  2. Student can predict what sc.nextLine() returns given the file's current cursor position, identify that the line terminator is consumed but not part of the return value, and recognize the three cases: non-empty line, empty line, partial last line: by what each returns.

    In this program: Each description is printed inside round brackets on a line of its own, so a terminator that came back with the description would break every item line. Measured: a version that assumes the terminator arrived and prints without a line break of its own runs the whole list together on one line. Check c4 ends without a final newline, so the last description is the partial last line.

    Not shown by a run:The empty-line case as a genuinely empty line in the input. The empty return is exercised, because the discarding read returns it once per item, and an empty description is left out of every check on purpose. An empty description and a description lost to the leftover newline print the same three characters, and a check that cannot tell those two apart is not a check.

  3. Student can choose between next(), nextInt(), nextDouble() for reading from a Scanner based on the expected token type, predict the exception thrown when the token cannot be parsed (InputMismatchException), and recognize the cursor-position rule that produces the trailing-whitespace trap.

    In this program: The quantity has to arrive as a number and the part name as a word, and the total line is what makes the number real. Measured: a version that assumes nextInt consumes the whole record line puts a second space in front of every part name, and a version that reads the description with next() ends the run with InputMismatchException.

    Not shown by a run:nextDouble, and the clause about predicting the exception. No value in this format is a decimal, because nextDouble reads the decimal mark from the runner's locale and a check that depends on the runner's locale fails for a reason a student cannot see. A correct run never meets a token that fails to parse either, so nothing a correct program prints names InputMismatchException. Two of the probes below end with it, which is the consequence of a wrong read rather than a student predicting the exception.

  4. Student can predict that a nextLine call immediately after a nextInt (or any token-mode read) returns the empty string, and apply one of the two standard fixes (discard newline with an extra nextLine, or stay in token mode).

    In this program: This is the whole program. Measured: leaving out the discarding read ends the run with InputMismatchException on any list of more than one item, and prints an empty description on a list of exactly one, so no check passes either way.

What to build

Write one program named SupplyList.

It reads a supply list from standard input.

The first line holds a whole number, which is how many items the list holds. There is at least one item.

Each item takes two lines. The first of the two holds the quantity, a space, and the part name, where the quantity is a whole number and the part name is a single word. The second of the two is the description, which is free text and may hold spaces and commas. Every description holds at least one character. The last line of the input may or may not end with a line break.

The program prints the list back.

The first line is the label items, a colon, one space, and how many items the list holds. After that comes one line per item, in the order the items arrived. An item line is the quantity, a space, a lowercase x, a space, the part name, a space, and then the description inside round brackets. The last line is the label total units, a colon, one space, and the sum of every quantity.

Print nothing else, and print no prompt.

The rest of the program is yours to shape.

One worked example

The first of the inputs, with what the program prints for it.

Three items with ordinary descriptions, every description holding a space and a comma

This is the shape a token read cannot deliver, so it separates a line read from a token read on the description.

Input
3
4 bolts
hex head, half inch
12 washers
flat, quarter inch
2 clamps
spring clamp
Output
items: 3
4 x bolts (hex head, half inch)
12 x washers (flat, quarter inch)
2 x clamps (spring clamp)
total units: 18

How your program is checked

Your program is run once for each input and its output is compared with what is shown. The worked example above is the first of them. You can run every one of them yourself before you hand anything in.

One item, the smallest list the spec allows

A missing discarding read does not end the run here the way it does on a longer list, and the empty description it prints instead is what this check catches.

Input
1
7 dowels
oak, six inch
Output
items: 1
7 x dowels (oak, six inch)
total units: 7

Five items, including a one-word description and a three-digit quantity

The one-word description is the case a token read on the description would get right, so a program that passes only on that item fails here on the rest.

Input
5
20 screws
phillips
3 hinges
brass, three inch
100 nails
common, two inch
1 level
twenty four inch
9 anchors
plastic
Output
items: 5
20 x screws (phillips)
3 x hinges (brass, three inch)
100 x nails (common, two inch)
1 x level (twenty four inch)
9 x anchors (plastic)
total units: 133

Two items, and the input ends without a line break after the last description

This is the partial last line, the third case the line-read observable names, and a program that only works on input ending in a line break fails here.

Input
2
6 shims
plastic, thin
15 nuts
zinc, quarter inch
Output
items: 2
6 x shims (plastic, thin)
15 x nuts (zinc, quarter inch)
total units: 21

What to watch for

Each one is copied from the notes for the rooms this challenge draws on, with what a run shows when it happens.

What this puts on your resume

You have written Java that reads a record spread across two lines of input, choosing a token read or a line read for each field and handling the newline that a token read leaves behind.

What the program needs where it runs

Notes for the author, not part of the student page

This block is here so the manifest can be reviewed on the page instead of in the file. It is not rendered on a student page.

Outcomes considered and left out

Why a run cannot see some clauses

Scope decisions

Carried in from earlier rooms

Assumed from earlier in the course

Where the expected output came from

Provenance