开发者

Java Simple Programming Help Reading from file, storing in hash map?

开发者 https://www.devze.com 2023-02-13 09:37 出处:网络
Anyone have any ideas on how to do this? 开发者_StackOverflowI am new and need to know how to implement this assignment. Really need advice on starting out with this project, thanks.

Anyone have any ideas on how to do this? 开发者_StackOverflowI am new and need to know how to implement this assignment. Really need advice on starting out with this project, thanks.

Here is the code:

package code;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 
 * This assignment involves character by character
 * processing, but this time the characters are not coming from a String, they
 * are coming from a file.
 * 
 * Because Java's file input classes throw various exceptions, which we do not
 * yet know how to handle, I am providing you with a class which deals with
 * those exceptions: UBFileReader. This is an iterator for a file.
 * You will be able to use this class during your write-up, but its source will
 * be hidden from you.
 * 
 * 
 * 
 * 
 * Overall, the project is a review viewer for various products. The information
 * about the reviews and products will be stored in a text file in the following format:
 * Products start with <p> and end with </p>
 * Reviews start with <c> and end with </c>
 * So given the input: <p>Mkay</p><c>Brown and hairy.</c>
 * The product would be: "Mkay"
 * The review would be: "Brown and hairy."
 * 
 * The products and reviews should be stored in a HashMap<String, LinkedList<String>>
 * where the key is the product and the value is a linked list containing all of the
 * reviews for the product.
 * 
 * The products and reviews and then displayed in a beautiful (light grey and yellow)
 * graphical user interface. The user can use this to browse the available products
 * and read their reviews. The user interface will be supplied for you, so all you
 * need to take care of is putting the appropriate information in the HashMap.
 * 
 */


public class TagFileParser {

    /**
     * Reads a file (identified by inputFilePath), one character at a time.
     * Products start with <p> and end with </p> as explained above. 
     * Reviews start with <c> and end with </c> as explained above.
     * Any text not inside of one of these tags should be ignored.
     * 
     * You may use only CharacterFromFileReader to read characters from the
     * input file.
     * 
     * In order to simplify the code writing experience, it is recommended
     * that you use a switch statement where the case would be the state.
     * This way, you only need to worry about what happens when you are at
     * that state. You should, however, fully understand the state diagram
     * as a whole and in parts, as you will be required to complete this
     * assignment next week at the beginning of lab.
     * 
     * @param String
     *            inputPath the path on the local filesystem to the input file
     * @returns a HashMap containing the product->linked list of reviews mappings.
     */

    public HashMap<String, List<String>> fillHashMap(String inputPath) {
        return null;
    }

}


User a BufferedReader to read the file. The BufferedReader allows you to read line by line.

Put in code that opens the file and reads a line at a time.

Don't worry about the rest of the assignment just get that far.

When that is working come back, for more help.


For starters, read through this example.

So when you read the input file, you need to check if the character you're reading is a '<' first. Then 'p'. Then '>' and so on.

Once you've understood how characters from files can be read into programs, read up on HashMap and LinkedList.

PS: UBFileReader (the class provided by your professor) will replace the phrase "throws Exception" in that code. Don't worry about this bit for now, get the rest of the program working first.


public static void main(String[] args) {

        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("C:\\testing.txt"));
            int i=0;
            HashMap<String, Integer> hm = new HashMap<String, Integer>();
            while ((sCurrentLine = br.readLine()) != null) {
            i++;
            hm.put(sCurrentLine,i);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消