Skip to content

Commit 9d3d9c6

Browse files
nshizirungudieumerci@gmail.comnshizirungudieumerci@gmail.com
authored andcommitted
added a parsons problem to section 8.3
1 parent 6c92935 commit 9d3d9c6

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,44 @@ public class CreateFile {
287287
<p>
288288
You may have noticed that there are some new methods you haven't seen yet. The <c>hasNextLine()</c> method checks if there is a next line in the file, and returns <c>false</c> if there isn't. This method allows us to iterate over every line till there is no next line. The <c>nextLine()</c> method of the Scanner object returns the next line in the file as a string.
289289
</p>
290+
291+
<exercise xml:id="java-read-file-parsons-exercise" label="java-read-file-parsons">
292+
<statement>
293+
<p>
294+
Construct the part of a Java program that opens a file with a <c>Scanner</c> and
295+
prints each line until there are no more lines. Drag the blocks into the correct order on the right.
296+
</p>
297+
</statement>
298+
<blocks>
299+
<block order="1">
300+
<choice correct="yes">
301+
<cline>try (Scanner fileReader = new Scanner(new File(filename))) {</cline>
302+
</choice>
303+
<choice correct="no">
304+
<cline>try (Scanner fileReader = new Scanner(filename)) {</cline>
305+
</choice>
306+
</block>
307+
308+
<block order="2">
309+
<choice correct="yes">
310+
<cline> while (fileReader.hasNextLine()) {</cline>
311+
</choice>
312+
<choice correct="no">
313+
<cline> while (fileReader.nextLine()) {</cline>
314+
</choice>
315+
</block>
316+
317+
<block order="3">
318+
<cline> String data = fileReader.nextLine();</cline>
319+
<cline> System.out.println(data);</cline>
320+
</block>
321+
322+
<block order="4">
323+
<cline> }</cline>
324+
<cline>}</cline>
325+
</block>
326+
</blocks>
327+
</exercise>
290328
</section>
291329

292330
<section xml:id="writing-to-files">

0 commit comments

Comments
 (0)