2011-02-26 4 views
0

누구든지이 작업을 수행하는 방법에 대한 아이디어가 있습니까? 나는 새롭고이 임무를 수행하는 방법을 알아야한다. 이 프로젝트를 시작하는 데 정말로 조언이 필요합니다. 감사합니다.Java Simple Programming 도움말 파일을 읽고 해시 맵에 저장 하시겠습니까?

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

} 
+1

선생님이 완전히 추위에 빠지셨습니까 ?? –

+3

프로그래밍 교사는 내가보기에도 시간이 갈수록 점점 악화되고 있습니다. "예외를 처리하는 방법을 알지 못하기 때문에 먼저 파일 I/O를 배우자!" –

+0

yea..they는 나와 함께 일할 많은 것을주지 않았다. 그냥 몇 가지 팁이 필요합니다 – user593301

답변

0

사용자가 파일을 읽을 수있는 BufferedReader로 : 여기

는 코드입니다. BufferedReader를 사용하면 한 줄씩 읽을 수 있습니다.

파일을 열고 한 번에 한 줄씩 읽는 코드를 입력하십시오.

과제의 나머지 부분에 대해서는 걱정하지 마십시오.

언제 다시 작동하는지 알려면 도움을 받으십시오.

+0

"한 번에 한 글자"를 읽어야하기 때문에 'BufferedReader'는 실제로 필요하지 않습니다. –

+0

자바를 사용하면 한 번에 한 문자 씩 읽을 수 있습니까? – user593301

0

처음에는 this example을 읽어주십시오.

그래서 입력 파일을 읽을 때 읽는 문자가 먼저 '<'인지 확인해야합니다. 그럼 'p'. 그렇다면 '>'등등.

파일에서 문자를 프로그램으로 읽는 방법을 이해했다면 HashMap 및 LinkedList에서 읽으십시오.

PS : UBFileReader (교수가 제공 한 클래스)는 해당 코드에서 "throws Exception"문구를 대체합니다. 지금은이 비트를 걱정하지 마십시오. 나머지 프로그램은 먼저 작동하십시오.

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

    } 
관련 문제