Skip to content

Commit 543f57c

Browse files
committed
add comments to the code block
1 parent ec32254 commit 543f57c

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -783,13 +783,16 @@ public class Histo {
783783

784784
<program xml:id="java-array" interactive="activecode" language="java" datafile="test.dat">
785785
<code>
786-
import java.util.Scanner;
787-
import java.io.File;
788-
import java.io.IOException;
786+
import java.util.Scanner; // import the Scanner class to read input from the user
787+
import java.io.File; // import the File class to read from files
788+
import java.io.IOException; // import the IOException class to handle file input/output exceptions
789+
/**
790+
* Program to read numbers from a file and produce a histogram using arrays in Java.
791+
*/
789792
public class HistoArray {
790793
public static void main(String[] args) {
791794
Scanner data = null;
792-
Integer[] count = {0,0,0,0,0,0,0,0,0,0};
795+
Integer[] count = {0,0,0,0,0,0,0,0,0,0}; // create an array of 10 integers initialized to 0
793796
Integer idx;
794797
try {
795798
data = new Scanner(new File("test.dat"));
@@ -801,7 +804,7 @@ public class HistoArray {
801804
}
802805
while(data.hasNextInt()) {
803806
idx = data.nextInt();
804-
count[idx] = count[idx] + 1;
807+
count[idx] = count[idx] + 1; // increment the count for the number read from the file, using array indexing
805808
}
806809
idx = 0;
807810
for(Integer i : count) {

0 commit comments

Comments
 (0)