diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index 99c5e927ce7e23559bf0ff4af6f7bb0b47ad2028..dff23eb518f543edb3b578d637e3fb49d1f06ab2 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -1087,24 +1087,26 @@ System.out.println("Failed!");</programlisting> <figure xml:id="sd1_fig_useScannerClass"> <title>User input recipe</title> - <programlisting language="java">try (final <link - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html">Scanner</link> scan = new <link - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html">Scanner</link>(<link - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/System.html#in">System.in</link>)){ - System.out.print("Enter a value:"); - final int value = scan.<link - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html#nextInt()">nextInt()</link>; - System.out.println("You entered " + value); -}</programlisting> + <programlisting language="java">import java.util.Scanner; +public class App { + public static void main(String[] args) { - <screen>Enter a value:1234567 -You entered 1234567</screen> + try (final Scanner scan = new Scanner(System.in)) { + System.out.print("Enter a value:"); + final int value = scan.nextInt(); + System.out.println("You entered " + value); + } + } +}</programlisting> <para>See <methodname xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html#nextBoolean()">nextBoolean()</methodname>, <methodname><link xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html#nextByte()">nextByte()</link></methodname> - and friends.</para> + and friends. Result:</para> + + <screen>Enter a value:1234567 +You entered 1234567</screen> </figure> <qandaset defaultlabel="qanda" xml:id="sw1QandaPostExamBonuspoints"> @@ -2028,22 +2030,18 @@ System.out.println("Do not copy!");</programlisting> </figure> <figure xml:id="sd1_fig_loopBadParameterization"> - <title>Hard to parameterize</title> - - <para>Limited workaround for runtime supplied repetition count:</para> + <title>Arbitrary number of repetitions</title> <programlisting language="java">System.out.print("Enter desired number of repetitions: "); -final repetitions = scan.nextInt(); - +final int repetitions = scan.nextInt(); switch(repetitions) { case 5: System.out.println("Do not copy!"); case 4: System.out.println("Do not copy!"); case 3: System.out.println("Do not copy!"); case 2: System.out.println("Do not copy!"); - case 1: System.out.println("Do not copy!"); -}</programlisting> + case 1: System.out.println("Do not copy!"); }</programlisting> - <para>Problem: Clumsy and limited (predefined maximum).</para> + <para>Limited and clumsy workaround.</para> </figure> <section xml:id="sd1_sect_while">