2013-04-09 2 views
0

주어진 파일을 읽고 파일을 목록으로 구문 분석하는 "Move To Front"인코더를 작성했습니다. 그것은 인코딩과 함께 잘 작동하지만 단 한 줄의 파일에서만 작동합니다. 문제는 while 루프에 있다고 생각합니다. splitArray는 while 루프 밖에 구문 분석자바로 텍스트 파일 읽기

while ((line = br.readLine()) != 
     null) // While the line file is not empty after reading a line from teh text file split the line into strings 
{ 
    splitArray = line.split(" "); 
} 

for (int i = 0; i <= splitArray.length - 1; 
    i++) // for every string in the array test if it exists already then output data accordinly 
{ 
    if (FirstPass.contains(splitArray[i])) { 
    System.out.println(FirstPass.lastIndexOf(splitArray[i])); 
    FirstPass.addFirst(splitArray[i]); 
    FirstPass.removeLastOccurrence(splitArray[i]); 
    } else if (!FirstPass.contains(splitArray[i])) { 
    FirstPass.addFirst(splitArray[i]); 
    System.out.println("0 " + splitArray[i]); 
    } 
} 

System.out.println(" "); 
for (String S : FirstPass) { 
    System.out.println(S); 
} 
+0

죄송 BR은 버퍼링 독자 –

+0

수정하시기 바랍니다 귀하의 게시물입니다 들여 쓰기를 수정하십시오. 탭을 적절한 수의 공백으로 바꾸십시오. –

+3

첫 번째 while 루프는 전체 파일을 읽고 마지막 줄을 제외한 모든 데이터를 삭제합니다. –

답변

0

귀하의 코드 .. 그래서 마지막 라인이 처리 얻을 것이다 : 여기

는 코드입니다.

모든 라인을 처리하기가 .. while 루프 내부 전체 for() 블록 있음을 넣어

while((line = br.readLine()) != null) // While the line file is not empty after reading a line from teh text file split the line into strings 
{ 
    splitArray = line.split(" "); 


    for(int i = 0; i <= splitArray.length - 1; i++)  // for every string in the array test if it exists already then output data accordinly 
    { 
     //.......... 
    } // end for 
} // end while 
+0

너무 작고 어리 석다는 것을 알았던 Kal에게 감사드립니다. 내 질문에 대한 답변을 hte 시간을내어 주셔서 감사합니다 –

0

잘못된 자리에 교정기 :

while((line = br.readLine()) != null) 
{ 
    splitArray = line.split(" "); 
} // This } shouuldn't be here... 
+0

중괄호를 제거하면 코드 컴파일 시간 오류가 발생합니다. – MathSquared

+0

@ MathSquared11235 100 % 정확함. 나는이 문제의 일반적인 영역을 지적하고 있으며, 지역화 된 코드에 완전한 코드를 제공하지는 않습니다. – John3136