Class MessageLoader

Object
MessageLoader

public class MessageLoader extends Object
This utility class contains methods that will read in a file as it is provided, and load the data into an ArrayList with OpenMessage objects inside of it.
  • Constructor Details

    • MessageLoader

      public MessageLoader()
  • Method Details

    • loadFile

      public static ArrayList<OpenMessage> loadFile(String filename)
      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

      public static String parseLine(String line)
      Takes in a line, in which one must grab the substring *after* the ":". It will also remove any leading and trailing spaces.

      Implementation Hints:

      Notice we are focusing on divide conquer glue. This allows us to test parseLine() before we even have to worry about reading a file!
      Parameters:
      line - line to substring and trim
      Returns:
      parsed line