2013-07-24 2 views
0

그래서 저는 채팅 클라이언트를 작성했으며 채팅 기록에서 특정 단어를 찾을 수 있도록 검색 기능을 만들기로 결정했습니다. 그러나 선택이 시작되는 줄 아래의 각 줄은 선택해야하는 단어의 바로 많은 색인입니다. 줄 바꿈 문자 또는 색인 위치가 누락 되었습니까?각 줄마다 색인을 추가하는 검색 기능

각 행의 색인이 어떻게 바뀌나요? 아니면 내 코드에 문제가 있습니다 :

private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {           
    toSearchText = searchText.getText(); 
    if (!toSearchText.equals("")) { 
     for (int i = index; i < searchBlock.length(); i++) { 
      if (searchBlock.charAt(i) == toSearchText.charAt(0)) { 
       System.out.println("found first char- Starting check loop." + i + j + "::" + count); 
       for (j = i; j < i + toSearchText.length(); j++) { 
        System.out.println("J" + j + " II " + innerIndex); 
        if (searchBlock.charAt(j) == toSearchText.charAt(innerIndex)) { 
         innerIndex++; 
         count++; 
         System.out.println("found char:" + innerIndex + " - " + searchBlock.charAt(j)); 
        } else { 
         System.out.println("Not the word"); 
         break; 
        } 
       } 
      } 
      System.out.println(i); 
      System.out.println(j); 

      if (count == toSearchText.length()) { 
       if (searchBlock.substring(i, j).equals(toSearchText)) { 
        System.out.println(searchBlock.substring(i, j) + " and " + toSearchText); 
        System.out.println("focusing"); 
        ClientWindow.mainText.requestFocusInWindow(); 
        ClientWindow.mainText.select(i, j); 
        count = 0; 
        innerIndex = 0; 
        index = i + toSearchText.length(); 
        if (index > searchBlock.length()) { 
         index = 0; 
        } 
        break; 
       } 
      } else { 
       System.out.println("focus refused"); 
      } 
     } 
     System.out.println("Search Finished"); 
    } 
}          

답변

0

좋아, 그래서 거기에 각 라인에 추가로 인덱스되는 문제를 해결하기 위해, 나는 그 값과를 minused 다음 라인을 계산하는 방법을 썼다 색인.

private int checkLine(int position){ 
    int counter = 0; 
    Scanner text = new Scanner(searchBlock.substring(0,position)); 

    while(text.hasNextLine()){ 
     counter++; 
     text.nextLine(); 
    } 
    return counter - 1; 
} 
관련 문제