2012-11-18 2 views
0

텍스트 파일에서 단어의 발생을 계산하고 특정 단어에 단어가 발견 된 시간을 반환하는 방법이 있습니다. 그러나 단어가있는 행 번호를 추적하지 않습니다. 나는 텍스트 파일의 줄 수를 세는 별도의 메서드를 가지고 있고 두 줄의 메서드를 줄 번호를 추적하는 메서드에 결합하여 각 줄에 단어를 기록하는 로그를 유지하려고합니다. 여기 회선 카운터 방법을 단어 계산 방법과 결합

내가 같은 결과 뭔가를 제공하기 위해 결합하고자하는 두 가지 방법이 여기에

public class Hash 
{ 
    private static final Object dummy = new Object(); // dummy variable 
    public void hashbuild() 
    { 
     File file = new File("getty.txt"); 
     // LineNumberReader lnr1 = null; 
     String line1; 
     try{ 
     Scanner scanner = new Scanner(file); 
     //lnr1 = new LineNumberReader(new FileReader("getty.txt")); 

     // try{while((line1 = lnr1.readLine()) != null) 
     // {}}catch(Exception e){} 

     while(scanner.hasNextLine()) 
     { 
      String line= scanner.nextLine(); 
      List<String> wordList1 = Arrays.asList(line.split("\\s+")); 
      Map<Object, Integer> hm = new LinkedHashMap<Object, Integer>(); 
      for (Object item : wordList1) 
      { 
       Integer count = hm.get(item); 
       if (hm.put(item, (count == null ? 1 : count + 1))!=null) 
       { 
        System.out.println("Found Duplicate : " +item); 
       } 
      } 
      for (Object key : hm.keySet()) 
      { 
       int value = hm.get(key); 
       if (value>1) 
       { 
        System.out.println(key + " occurs " + (value) + " times on line # "+lnr1.getLineNumber()); 
       } 
      } 
     } 
    } catch (FileNotFoundException f) 
    {f.printStackTrace();} 
    } 
} 

"단어 줄 Y에서 X 번 발생"내 원래 라인 계산 방법입니다

public void countLines() 
{ 
    LineNumberReader lnr = null; String line; 
    try 
    { 
     lnr = new LineNumberReader(new FileReader("getty.txt")); 
     while ((line = lnr.readLine()) != null) 
     { 
     System.out.print("\n" +lnr.getLineNumber() + " " +line); 
     } 
     System.out.println("\n"); 
    }catch(Exception e){} 
} 
+0

왜 주석 처리 된 코드를 사용하지 않으십니까? 그것은 당신을 위해 줄을 셀 것입니다. 문제가 있거나 다른 접근법을 요구하고 있습니까? –

+0

주석 처리 된 코드는 내가 정말로 찾고있는 것을 제공하지 않습니다. 그것은 줄 번호를 추적하지만, 나는 다른 방법으로 올인원 방법으로 그것을 결합하고 싶습니다. 나는 그것이 작동하지 않았기 때문에 그것을 주석 처리했다. 그러나 나는 당신이 내가 가려고 노력하고 있었던 곳을 볼 수 있었을 정도로 그것을 놓고 갔다. 내가 묻는 모든 질문을 받아 들일 수있을 정도로 – user1660948

+0

@Ankur? 상관없이 나를 돕는거야? – user1660948

답변

0

while 회 돌이에서 줄 번호를 기억하지 그래? 새 변수를 초기화하고 nextline을 호출 할 때 늘리십시오.

+0

나는 그걸 어디에서 할 것인지 잘 몰랐습니다. scanner.nextline 다음에 – user1660948

+0

을 실행하는 몇 개의 루프가 있습니다. – sorencito

+0

라인 16 : int linenumber = 0;/* 다음 스위치를 누른 다음 줄 18 */linenumber ++; – sorencito