"defaultResponses.txt", and the entries added to the ArrayList would Parameter "defaultResponses.txt', you should see printed:Īt your disposal, you now only need to insert a few remaining linesĪt the end distinguishes this method name, indicates that more than oneĭiscussed above, an example would be when fileName is To ease testing of this method, I added a tempory testing method intoįileUtil: testReadParagraph. Making sure you advance the Scanner to the right place is very Throw away the empty line in the input after the paragraph. String after each nonempty line is added. So be sure to explicitly add a newline back onto your paragraph Throws away the following newline in the input. Public static String readParagraph(Scanner in) * ending with the newline after the last nonempty line. * The whole paragraph, with the newlines after each line, * The Scanner stops reading after the first empty line. * by an empty line, and return a single String, preserving the newlines. * Read a paragraph consisting of one or more nonempty lines terminated Code the body of the first method in the class FileUtil: Other contexts, let us first handle one paragraph at a Hence I imagineįile defaultResponses.txt to contain a number of paragraphs. To mark the end of a paragraph in plain text. This way multiple lines in one response are visuallyĪnd logically distinguished from the next response. The lines up a little differently to provide more multiline responses Unfortunately it is not visible inside the BlueJĬan click on it in an operating system directory window. Look at the file defaultResponses.txt in the projectĭirectory.Be sure to read the examplesĪnd instructions carefully (several times). The first task is to write a helping method that will be useful forįew lines of code. Method, dealing with the possibility of a null value being returned. I have also written the code at the beginning of both readParagraphs Public static Scanner openFileScanner(String fileName) * or else null if there is an error opening the file. * A Scanner ready to read from the specified file, * The name of a text file accompanying this class. * Create and return a Scanner coming from a named text file. That is not a focus of this course, so I have Just wait for input rather than returning false. Keyboard, however, the loop would go on forever: Even if Look at the code above carefully and see what it is doing! You will need this. Would print out the whole file in upper case and terminateĪppropriately. Methods like hasNextLine() and hasNext() to check for the end of the file. They have a definite, predetermined end, so it makes sense to use Reading from a file is easier than reading from the We will use the ability to read from a text The keyboard, but an explicit String or a text file are among the Provide simple versatile methods to fill an ArrayListĭata itself into a text file, with the only overhead being a few extraĬould save time later in your game project, too, so the general-purposeĬode is all placed in a new utility class FileUtil.Ĭlass I gave you you and look at the very short fillDefaultResponses andĮach by using calls to two methods in the class FileUtil thatįileUtil.readParagraphs("defaultResponses.txt", defaultResponses) įileUtil.readStringMappings("responseMap.txt", responseMap) įile call defaultResponses.txt to the ArrayList defaultResponsesįrom the file responseMap.txt to the HashMap responseMap.Ī Scanner can read from a variety of text sources. There is all the repetitious code to make multiline Open the original Responder class, named ResponderOrig in the lab andĪll the strings for the responses are pre-coded for you there, but if Original Responder is included as ResponderOrig for Modification of the chapter 5 Tech Support2-j5Ĭlass Go, and Responder now refers to a class FileUtil.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |