2014-05-21 1 views
0

안녕 나는찾기 무리

1-1, 
2-3, 
4-10, 
11-20 

지금은 범위에있는 모든 디렉토리 말할 1-10 그래서 나에게 반환해야 DIRS 1을 찾으려면 같은 이름을 가진 하위 디렉토리가 포함 디렉토리가 -1,2-3 및 4-10. 다음 코드는 있지만 예상대로 작동하지 않습니다.

File files[] = folder.listFiles(new FileFilter() { 
      public boolean accept(File file) { 

       String name = file.getName().toLowerCase(); 

       if (name.startsWith("1-") || name.endsWith("-10")) 
        return true; 

       return false; 
      } 
     }); 

위 코드는 출력 1-1과 4-10을 제공하며 2-3을 조합하여 포함하지 않습니다. 이 문제를 어떻게 해결합니까? 도와주세요. 미리 감사드립니다.

+0

'이름'을 '최소'및 '최대'값으로 나누고 범위를 확인하는 것이 더 쉽지 않습니까? – Perneel

+1

2-3이 'if'문에있는 조건과 일치하지 않습니다. – Rebecca

답변

1

숫자 기준에 일치 시키려면 이름을 문자열로 검사하는 것이 올바른 방법이 아닙니다. 위에서 @Perneel이 말했듯이 디렉토리 이름을 구문 분석하여 포함 된 범위를 가져 와서 확인합니다.

File[] files = folder.listFiles(new FileFilter() { 
     public boolean accept(File file) { 
      try { 
       String[] bounds = file.getName().toLowerCase().split("-"); 
       return (Integer.parseInt(bounds[0]) <= 10 && Integer.parseInt(bounds[1]) >= 1); 
      } catch (Exception e) { 
       // array index out of bounds & number format exceptions mean 
       // this isn't a directory with the proper name format 
       return false; 
      } 
     } 
    }); 
    System.out.println(Arrays.toString(files)); // 1-1, 2-3, 4-10