Skip to content

Commit 0dcbed7

Browse files
committed
added listing blocks
1 parent 33e6cc3 commit 0dcbed7

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,8 @@ void main() {
527527
Next, let’s look at a program which reads numbers from a file and produces a histogram showing the frequency of the numbers. The data file we will use has one number between 0 and 9 on each line of the file. Here is a simple Python program that creates and prints a histogram.
528528
</p>
529529

530-
531-
<program xml:id="python-input-file1" interactive="activecode" language="python">
530+
<listing xml:id="python-input-file1">
531+
<program interactive="activecode" language="python">
532532
<code>
533533
def main():
534534
count = [0]*10
@@ -542,11 +542,13 @@ def main():
542542
main()
543543
</code> <tests> </tests>
544544
</program>
545+
</listing>
545546

546547
<p>
547548
Test running the program. It will read this data:
548549
</p>
549-
<datafile label="datafile-test" filename="test.dat" editable="no" cols="12" rows="6">
550+
551+
<datafile xmlid="datafile-test" label="datafile-test" filename="test.dat" editable="no" cols="12" rows="6">
550552
<pre>
551553
1
552554
2
@@ -625,7 +627,8 @@ Here is the Java code needed to write the exact same program:
625627
</p>
626628
</note>
627629

628-
<program xml:id="java-null-object" interactive="activecode" language="java" datafile="test.dat">
630+
<listing xml:id="java-null-object">
631+
<program interactive="activecode" language="java" datafile="test.dat">
629632
<code>
630633
import java.util.Scanner;
631634
import java.util.ArrayList;
@@ -661,6 +664,7 @@ public class Histo {
661664
}
662665
</code> <tests> </tests>
663666
</program>
667+
</listing>
664668

665669
<p>
666670
Before going any further, I suggest you try to compile the above program and run it on some test data that you create.
@@ -674,10 +678,14 @@ public class Histo {
674678
Technically, you don&#x2019;t <em>have</em> to declare what is going to be in an array list. The compiler will allow you to leave the <c>&lt;``*Type*</c>&gt;`` off the declaration. If you don&#x2019;t tell Java what kind of object is going to be on the list Java will give you a warning message like this:
675679
</p>
676680

677-
<pre>
681+
<listing xml:id="java-arraylist-warning">
682+
<program>
683+
<code>
678684
Note: Histo.java uses unchecked or unsafe operations.
679685
Note: Recompile with -Xlint:unchecked for details.
680-
</pre>
686+
</code>
687+
</program>
688+
</listing>
681689

682690
<p>
683691
Without the <c>&lt;Integer&gt;</c> part of the declaration Java simply assumes that <em>any</em> object can be on the list. However, without resorting to an ugly notation called casting, you cannot do anything with the objects on a list like this! So, if you forget you will surely see more errors later in your code. (Try it and see what you get)
@@ -686,14 +694,18 @@ public class Histo {
686694
<p>
687695
Lines 13&#x2014;20 are required to open the file. Why so many lines to open a file in Java? The additional code mainly comes from the fact that Java forces you to reckon with the possibility that the file you want to open is not going to be there. If you attempt to open a file that is not there you will get an error. A try/catch construct allows us to try things that are risky, and gracefully recover from an error if one occurs. The following example shows the general structure of a try/catch block.
688696
</p>
689-
690-
<pre>
697+
698+
<listing xml:id="java-try-catch">
699+
<program>
700+
<code>
691701
try {
692702
Put some risky code in here, like opening a file
693703
} catch (Exception e) {
694704
If an error happens in the try block an exception is thrown. We will catch that exception here!
695705
}
696-
</pre>
706+
</code>
707+
</program>
708+
</listing>
697709

698710
<p>
699711
Notice that in line 16 we are catching an <c>IOException</c>. In fact, we will see later that we can have multiple catch blocks to catch different types of exceptions. If we want to be lazy and catch any old exception we can catch an <c>Exception</c> which is the parent of all exceptions. However, catching <c>Exception</c> is a terrible practice, since you may inadvertently catch exceptions you do not intend to, making it harder to identify bugs in your program.

0 commit comments

Comments
 (0)