Menu

[Solved]Method Parses Story Adventure File Method Read Contents Scanner Line Line Populate Paralle Q37036538

/** * This method parses a story adventure file. * * The methodwill read the contents from the Scanner, line by line, and populatethe parallel * ArrayLists rooms and trans. As such the story fileshave a specific structure. The order of * the rooms in the storyfile correspond to the order in which they will be stored in the *parallel ArrayLists. * * When reading the file line-by-line,whitespace at the beginning and end of the line should be *trimmed. The file format described below assumes that whitespacehas been trimmed. * * Story file format: * * – Any line (outside ofa room’s description) that begins with a ‘#’ is considered acomment * and should be ignored. * – Room details begin with a linestarting with ‘R’ followed by the room id, terminated with * a ‘:’.Everything after the first colon is the room title. The substringsof the room id * and the room title should be trimmed. * – The roomdescription begins on the line immediate following the lineprefixed with ‘R’, * containing the room id, and continues until aline of “;;;” is read. * – The room description may be multi-line.Every line after the first one, should be * prefixed with a newlinecharacter (‘n’), and concatenated to the previous description *lines read for the current room. * – The room transitions beginimmediately after the line of “;;;”, and continue until a line *beginning with ‘R’ is encountered. There are 3 types of transitionlines: * – 1 — Terminal Transition: A terminal transition iseither Config.SUCCESS or * Config.FAIL. This room is the end of thestory. * This value should be stored as a transition with theString at * index Config.TRAN_DESC set to the value read. The restof the * Strings in the transition String array should be null. * Aroom with a terminal transition can only have one transition *associated with it. Any additional transitions should result in * aparse error. * – 2 — Normal Transition: The line begins with ‘:’followed by the transition description, * followed by ” -> “(note the spaces), followed by the room id to * transition to. Fornormal transitions (those without a transition * weight), set thevalue at index Config.TRAN_PROB to null. * – 3 — WeightedTransition: Similar to a normal transition except that there is a *probability weight associated with the transition. After the * roomid (as described in the normal transition) is a ‘?’ * followed bythe probability weight. * – You can assume that room ids do notcontain a ‘?’. * – You can assume that Config.SUCCESS andConfig.FAIL do not start with a ‘:’. * * In the parallel ArrayListsrooms and trans, the internal structures are as follows: * * TheString array structure for each room has a length ofConfig.ROOM_DET_LEN. The entries in * the array are as follows: *Index | Description * ——————————————–* Config.ROOM_ID | The room id * Config.ROOM_TITLE | The room’stitle * Config.ROOM_DESC | The room’s description * * The Stringarray structure for each transition. Note that each room can havemultiple * transitions, hence, the ArrayList of ArrayLists ofString[]. The length of the String[] is * Config.TRAN_DET_LEN. Theentries in the String[] are as follows: * Index | Description *——————————————————————* Config.TRAN_DESC | The transition description *Config.TRAN_ROOM_ID | The transition destination (id of the room) *Config.TRAN_PROB | The probability weight for the transition * * Ifyou encounter a line that violates the story file format, themethod should print out an * error message, terminated by a newline, to System.out displaying: * “Error parsing file on line:lineNo: lineRead”, where lineNo is the number of lines read * bythe parseStory method (i.e. ignoring the magic number if Milestone#3), and lineRead is * the offending trimmed line read from theScanner. * * After parsing the file, if rooms or trans have zerosize, or they have different sizes, print * out an error message,terminated by a new line, to System.out displaying: * “Errorparsing file: rooms or transitions not properly parsed.” * * Afterparsing the file, if curRoom is not null, store the reference ofthe id of the room at * index 0 of the rooms ArrayList into thecell at index 0 of curRoom. * * Hint: This method only needs asingle loop, reading the file line-by-line. * * Hint: Tosuccessfully parse the file, you will need to maintain a state ofwhere you are in * the file. I.e., are you parsing the description,parsing the transitions; is there an error; etc? One suggestionwould be to use an enum to enumerate the different states. * *@param sc The Scanner object buffering the input file to read. *@param rooms The ArrayList structure that will contain the roomdetails. * @param trans The ArrayList structure that will containthe transition details. * @param curRoom An array of at leastlength 1. The current room id will be stored in the cell * at index0. * @return false if there is a parsing error. Otherwise, true.*/

public static boolean parseStory(Scanner sc, ArrayList rooms,ArrayList > trans, String[] curRoom) { return true; }

Expert Answer


Answer to /** * This method parses a story adventure file. * * The method will read the contents from the Scanner, line by line, a… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *