From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch10;
02
03 importimport means to make the classes and/or packages available in this program java.io.File;
04 importimport means to make the classes and/or packages available in this program java.io.FileNotFoundException;
05 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
06 importimport means to make the classes and/or packages available in this program java.util.Scanner;
07
08 /**
09 * Non-FANG program: A TextFileByWord object reads a file into a list.
10 * The listLoadFromFile(fname) method reads the contents of the file
11 * word by word, storing the results in the strings ArrayList.
12 */
13 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects TextFileByWord {open braces start code blocks and must be matched with a close brace
14 /**
15 * collection containing all of the strings (words) read from the file
16 */
17 ArrayList<String> strings;
18
19 /**
20 * Construct a TextFileByWord object; initialize the strings list.
21 */
22 publicpublic is used to indicate unrestricted access (any other class can have access) TextFileByWord() {open braces start code blocks and must be matched with a close brace
23 strings =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<String>();
24 }close braces end code blocks and must match an earlier open brace
25
26 /**
27 * The argument list should consist of exactly one file name. Thus the
28 * length is checked and ifif executes the next statement only if the condition in parenthesis evaluates to true it is right then a TextFileByWord object
29 * is created and used to read and print out information about the
30 * contents of the named file.
31 *
32 * @paramthis is the Javadoc tag for documenting the purpose of parameters args the command-line arguments; should have one file name
33 */
34 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args) {open braces start code blocks and must be matched with a close brace
35 ifif executes the next statement only if the condition in parenthesis evaluates to true (args.length ==this is the comparison operator which evaluates to true if both sides are the same 1) {open braces start code blocks and must be matched with a close brace
36 String fname =this assignment operator makes the left side equal to the right side args[brackets are typically used to declare, initialize and index (indicate which element of) arrays0]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
37 TextFileByWord byWord =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor TextFileByWord();
38 byWord.listLoadFromFile(fname);
39 System.out.println("----- listPrint -----");
40 byWord.listPrint();
41 System.out.println("----- listPrint -----");
42 System.out.println("There were " +adds two numbers together or concatenates Strings together byWord.listSize() +adds two numbers together or concatenates Strings together
43 " words in " +adds two numbers together or concatenates Strings together fname);
44 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
45 System.out.println(
46 "Usage: provide exactly one (1) file name on command-line.");
47 System.out.println("Program Terminating");
48 }close braces end code blocks and must match an earlier open brace
49 }close braces end code blocks and must match an earlier open brace
50
51 /**
52 * Create a newnew is used to create objects by calling the constructor strings list and fill it from the file. If there are
53 * any problems, the contents of strings is left unchanged.
54 *
55 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname
56 */
57 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value listLoadFromFile(String fname) {open braces start code blocks and must be matched with a close brace
58 File file =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor File(fname);
59 ifif executes the next statement only if the condition in parenthesis evaluates to true (file.exists() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) file.canRead()) {open braces start code blocks and must be matched with a close brace
60 trytry is for executing a code block that may experience exceptions (errors) {open braces start code blocks and must be matched with a close brace
61 ArrayList<String> localStrings =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<String>();
62 Scanner scanner =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(file);
63 String word;
64 whilewhile is a looping structure for executing code repeatedly (scanner.hasNext()) {open braces start code blocks and must be matched with a close brace
65 word =this assignment operator makes the left side equal to the right side scanner.next();
66 localStrings.add(word);
67 }close braces end code blocks and must match an earlier open brace
68 strings =this assignment operator makes the left side equal to the right side localStrings;
69 scanner.close();
70 }close braces end code blocks and must match an earlier open brace catchcatch means to handle an exception that may occur (FileNotFoundException e) {open braces start code blocks and must be matched with a close brace
71 System.out.println("PANIC: This should never happen!");
72 e.printStackTrace();
73 }close braces end code blocks and must match an earlier open brace
74 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
75 System.out.println("Unable to open \"" +adds two numbers together or concatenates Strings together fname +adds two numbers together or concatenates Strings together "\" for input");
76 }close braces end code blocks and must match an earlier open brace
77 }close braces end code blocks and must match an earlier open brace
78
79 /**
80 * Print out the contents of the strings list, one entry per line.
81 */
82 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value listPrint() {open braces start code blocks and must be matched with a close brace
83 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i !=this is the not equals operator which evaluates to true if both sides are different strings.size(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
84 System.out.println(strings.get(i));
85 }close braces end code blocks and must match an earlier open brace
86 }close braces end code blocks and must match an earlier open brace
87
88 /**
89 * Get the number of words stored.
90 *
91 * @returnnull the number of words in strings
92 */
93 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer listSize() {open braces start code blocks and must be matched with a close brace
94 returnreturn means to provide the result of the method and/or cease execution of the method immediately strings.size();
95 }close braces end code blocks and must match an earlier open brace
96 }close braces end code blocks and must match an earlier open brace
97
98 //Uploaded on Mon Mar 29 21:39:48 EDT 2010
|
Download/View scg/ch10/TextFileByWord.java