2012-04-21 3 views
0

에 계산에 대해 내가 txt 파일
MYFILE.TXT자바 : txt 파일

ABC,xyzwegwegwe 
ABC,12312312312 
ABC,sdfsdf3sdfs 


어떻게 내가 단어 "ABC"를 셀 수에 일부 단어를 계산하고 싶으십니까?
출력 : "ABC" have: 3

while (myfile.hasNextLine()) { 
      line = myfile.nextLine(); 
      lines.add(line); 
        if(xxxxx){ //if have ABC, words++ 
         words++; 
        } 
     } 
System.out.print("\"ABC\" have: "+words); 

답변

3

나는 당신이 일을하려고하는 것은 생각 (그리고 그것은 단지 한 줄에 "ABC"의 사본이있는 경우)이 확인되지 않습니다

if(line.contains("ABC")) 
{ 
    words++; 
} 


String lineToTest = "ABC , sdq2we9ieorwq , EFG" 

if(line.contains("ABC")) 
{ 
    words++; 
} 

if(line.contains("EFG")) 
{ 
    words++; 
} 

공지 사항 dupliates !!!

+0

작동합니까, 여러 단어 수 있습니까? 예 : count "ABC", "EFG" – heyman

+0

@heyman "단어"가 무엇인지 구분하거나 정의 할 수 있습니까? – Kevin

+0

String.contains는 라인에 특정 세트가 들어 있는지 확인합니다. – Kevin