From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch09;
002
003 importimport means to make the classes and/or packages available in this program java.io.File;
004 importimport means to make the classes and/or packages available in this program java.io.FileNotFoundException;
005 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
006 importimport means to make the classes and/or packages available in this program java.util.Scanner;
007
008 /**
009 * Non-FANG program: A TextFileByWord object reads a file into a list.
010 * The listLoadFromFile(fname) method reads the contents of the file
011 * word by word, storing the results in the strings ArrayList.
012 *
013 * @authorthis is the Javadoc tag for documenting who created the source code blad
014 */
015 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
016 /**
017 * collection containing all of the strings (words) read from the file
018 */
019 ArrayList<String> strings;
020
021 /**
022 * Construct a TextFileByWord object; initialize the strings list.
023 */
024 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
025 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>();
026 }close braces end code blocks and must match an earlier open brace
027
028 /**
029 * Create a newnew is used to create objects by calling the constructor strings list and fill it from the file. If there are
030 * any problems, the contents of strings is left unchanged.
031 *
032 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname
033 */
034 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
035 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);
036 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
037 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
038 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>();
039 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);
040 String word;
041 whilewhile is a looping structure for executing code repeatedly (scanner.hasNext()) {open braces start code blocks and must be matched with a close brace
042 word =this assignment operator makes the left side equal to the right side scanner.next();
043 localStrings.add(word);
044 }close braces end code blocks and must match an earlier open brace
045 strings =this assignment operator makes the left side equal to the right side localStrings;
046 scanner.close();
047 }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
048 System.out.println("PANIC: This should never happen!");
049 e.printStackTrace();
050 }close braces end code blocks and must match an earlier open brace
051 }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
052 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");
053 }close braces end code blocks and must match an earlier open brace
054 }close braces end code blocks and must match an earlier open brace
055
056 /**
057 * Print out the contents of the strings list, one entry per line.
058 */
059 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
060 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
061 System.out.println(strings.get(i));
062 }close braces end code blocks and must match an earlier open brace
063 }close braces end code blocks and must match an earlier open brace
064
065 /**
066 * Get the number of words stored.
067 *
068 * @returnnull the number of words in strings
069 */
070 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
071 returnreturn means to provide the result of the method and/or cease execution of the method immediately strings.size();
072 }close braces end code blocks and must match an earlier open brace
073
074 /**
075 * The argument list should consist of exactly one file name. Thus the
076 * 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
077 * is created and used to read and print out information about the
078 * contents of the named file.
079 *
080 * @paramthis is the Javadoc tag for documenting the purpose of parameters args the command-line arguments; should have one file name
081 */
082 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
083 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
084 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;
085 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();
086 byWord.listLoadFromFile(fname);
087 System.out.println("----- listPrint -----");
088 byWord.listPrint();
089 System.out.println("----- listPrint -----");
090 System.out.println("There were " +adds two numbers together or concatenates Strings together byWord.listSize() +adds two numbers together or concatenates Strings together
091 " words in " +adds two numbers together or concatenates Strings together fname);
092 }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
093 System.out.println(
094 "Usage: provide exactly one (1) file name on command-line.");
095 System.out.println("Program Terminating");
096 }close braces end code blocks and must match an earlier open brace
097 }close braces end code blocks and must match an earlier open brace
098 }close braces end code blocks and must match an earlier open brace
099
100 //Uploaded on Mon Mar 29 21:41:21 EDT 2010
|
Download/View scg/ch09/TextFileByWord.java