Class MessageLoader
OpenMessage
objects inside of it.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ArrayList<OpenMessage>
This method reads through a file, and builds a number of Message objects based on the contents of that file.static String
Takes in a line, in which one must grab the substring *after* the ":".
-
Constructor Details
-
MessageLoader
public MessageLoader()
-
-
Method Details
-
loadFile
This method reads through a file, and builds a number of Message objects based on the contents of that file. It then returns an ArrayList for each Message.For this file, you can assume the following format:
TO: name FROM: name SUBJECT: short subject BODY: single line message, no need to worry about \n
Every four lines will be there, and they will be setup in sets of four. As such, *if* a line exists, you can assume four lines.
For example:
TO: Alice FROM: Hatter SUBJECT: Red Queen BODY: To keep ahead, run from the red queen. TO: Hatter FROM: Red SUBJECT: New Hat BODY: Hatter, I need a new hat, and if I don't get it, you won't need one. *Kisses* Queen.
Implementation Suggestions
First make sure to initialize the arraylist, and just return the empty list. I would suggest compiling at that point just to make sure you don't have a typo.
Second, work through reading the file. It will be a loop that loops until there are no more lines.
Scanner.hasNextLine()
Remember, when you read the file, you can assume four lines at a time. Here is some sample code to help you.
// while inside the loop, that loops *while* scanner.hasNextLine()... String to = parseLine(scanner.nextLine()); String from = parseLine(scanner.nextLine()); // some of your code here to complete this messageList.add(new OpenMessage(to, from, sub, body)); // assuming I called my ArrayList messageList
Be aware of the other supporting methods we are having you implement (hint, implement and test them first!). Also remember that when scanning from a file, use a try/catch block. If there is no error, catch the FileNotFoundException!
- Parameters:
filename
- Name of file to be opened.- Returns:
- An ArrayList of type Message with 0 or more messages.
-
parseLine
Takes in a line, in which one must grab the substring *after* the ":". It will also remove any leading and trailing spaces.Implementation Hints:
- Use indexOf(), but don't forget to double check that it works due to OB1 errors!
String.indexOf(String)
- Use
String.trim()
after you get the substring! -
String.substring(int)
will prove helpful.
- Parameters:
line
- line to substring and trim- Returns:
- parsed line
- Use indexOf(), but don't forget to double check that it works due to OB1 errors!
-