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.
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.
Student can choose between
Scannertoken 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.
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.
Student can choose between
next(),nextInt(),nextDouble()for reading from aScannerbased 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
nextIntconsumes the whole record line puts a second space in front of every part name, and a version that reads the description withnext()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, becausenextDoublereads 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 namesInputMismatchException. Two of the probes below end with it, which is the consequence of a wrong read rather than a student predicting the exception.Student can predict that a
nextLinecall immediately after anextInt(or any token-mode read) returns the empty string, and apply one of the two standard fixes (discard newline with an extranextLine, 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.
3
4 bolts
hex head, half inch
12 washers
flat, quarter inch
2 clamps
spring clamp
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.
1
7 dowels
oak, six inch
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.
5
20 screws
phillips
3 hinges
brass, three inch
100 nails
common, two inch
1 level
twenty four inch
9 anchors
plastic
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.
2
6 shims
plastic, thin
15 nuts
zinc, quarter inchitems: 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.
blaming
nextLinefor "returning nothing"What a run shows: On c1, c3 and c4 the run ends with InputMismatchException before anything prints, because with no discarding read the next quantity read lands on a description. On c2 the run finishes and prints one item with an empty description and the right total, which is the same list with nothing in the brackets.
adding
sc.nextLine()flushes in the wrong placeWhat a run shows: Every item line prints with empty brackets and every quantity and the total are right, so the run looks healthy and every check fails on the same missing text.
trying to fix the trap with
sc.skip(...)What a run shows: Nothing, on this platform. Measured:
skip("\n")in place of the discarding read produced output identical to the reference on all four checks, because the input holds LF line breaks and the pattern matches one exactly. The leaf objects to the idiom for being fragile across platforms rather than for failing here, and the checks agree with the leaf.assuming
nextInt()reads a whole lineWhat a run shows: Every item line carries a second space in front of the part name, because a line read after the quantity returns the rest of that line with its leading space still on it. Every quantity, description and total is right, so the only wrong thing on the page is a space.
mixing
next()andnextInt()and expecting them to behave likenextLine()What a run shows: The run ends with InputMismatchException before anything prints, because a token read takes only the first word of the description and the next quantity read lands on the second word.
assuming an empty
Stringreturn means EOFWhat a run shows: The list stops after the first item. Every later item line prints a zero quantity and the word null twice, and the total is the first quantity alone.
expecting
nextLine()to include the\nWhat a run shows: Every item and the total run together on one line, because a program written on that assumption leaves out the line break of its own that the output needs.
mixing
hasNextIntwithnextLinereadsWhat a run shows: Nothing, with these four inputs. Measured: replacing the counted loop with a
hasNextIntguard produced output identical to the reference on all four checks, because no description line in any of them begins with a number. A fifth input whose description began with a number would separate the two, and writing one would make the guard the subject of the challenge rather than the reads.
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
- The sandbox has to supply the check input on standard input.
- This program creates no file and reads none, so it needs no writable folder and no folder of its own per check.
- Not yet confirmed on the deployed runner. This was verified locally only. No call was made to the Judge0 host from this pass.
Mark it built
Nothing on this page runs your program. You run it yourself, once for each input above, and compare what it prints with what is shown. When all of them match, check the box. The mark is your own note about your own work, and it stays in this browser.
Unchecking the box takes the mark back off. Each outcome above then reads whatever the rest of your work shows.