[英]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.