繁体   English   中英

Java简单编程帮助从文件读取,存储在哈希图中?

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

有人对如何执行此操作有任何想法吗? 我是新来的,需要知道如何实现这项任务。 真的非常需要有关启动此项目的建议。

这是代码:

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;
    }

}
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();
            }
        }

    }

用户使用BufferedReader读取文件。 BufferedReader允许您逐行读取。

输入打开文件并一次读取一行的代码。

不用担心剩下的任务就那么多了。

工作正常后,请返回以获取更多帮助。

对于初学者,请通读本示例

因此,当您读取输入文件时,您需要先检查所读取的字符是否为'<'。 然后是“ p”。 然后是“>”,依此类推。

了解了如何将文件中的字符读入程序后,请在HashMap和LinkedList上阅读。

PS:UBFileReader(由您的教授提供的课程)将替换该代码中的“ throws Exception”一词。 现在不用担心这一点,先让程序的其余部分开始工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM