2012-05-14 3 views
1

질문 : 입력 파일을 매개 변수로 나타내는 스캐너를 허용하고 60 자보다 긴 모든 줄을 단어 줄 바꿈하여 파일의 각 줄을 콘솔에 출력하는 wordWrap이라는 메서드를 작성합니다. 예를 들어 한 줄에 112자를 포함하는 경우이 메서드는 처음 60자를 포함하는 줄과 마지막 52자를 포함하는 줄로 바꿔야합니다. 217 개 문자를 포함하는 라인은 네 개의 선으로 래핑한다 : 길이 60 길이의 최종 라인의 세 37스캐너를 사용한 문자열 처리

내 코드 : 출력에

public void wordWrap(Scanner input) { 

    while(input.hasNextLine()){ 
     String line=input.nextLine(); 
     Scanner scan=new Scanner(line); 
     if(scan.hasNext()){ 
      superOuter: 
      while(line.length()>0){ 

       for(int i=0;i<line.length();i++){ 
        if(i<60 && line.length()>59){ 
         System.out.print(line.charAt(i)); 

        } 
        //FINISH OFF LINE<=60 characters here 

        else if(line.length()<59){ 

         for(int j=0;j<line.length();j++){ 
          System.out.print(line.charAt(j)); 

         } 
         System.out.println(); 

         break superOuter; 
        } 
        else{ 
         System.out.println(); 

         line=line.substring(i); 

         break ; 
        } 


       } 

      } 
     } 
     else{ 
      System.out.println(); 
     } 

    } 
} 

문제점 :

예상 출력 :

 
Hello 
How are you 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 
I am fine 

Thank you 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 

The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 

This line is exactly sixty characters long; how interesting! 

Goodbye 

생산 출력 : 내가 잘못 했습니까

 
Hello 
How are you 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 
I am fine 

Thank you 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 

The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 
The quick brown fox jumps over the lazy dog the quick brown 
fox jumps over the lazy dog 

This line is exactly sixty characters long; how interesting!This line is exactly sixty characters long; how interesting!This line is exactly sixty characters long; how interesting!... 
*** ERROR: excessive output on line 13 

???? 당신이 외부 while 루프 (과에서 휴식을 원하는 반면, 당신은 단지 내부 for 루프에서 깨고있다 (정확히 60 자 라인의 경우) else 상태에서

+1

입력 (스캐너)의 줄을 반복하고, 60 자보다 긴지 확인하고, 처음 60 자 이하를 인쇄하고, 60 자 이하의 부분 문자열을 끝까지 가져오고, 그 길이가 0이 될 때까지 반복하시오. – Hidde

답변

0

따라서, 동일한를 작성하는 끝 라인 60 회).

줄을 59 자 미만으로 사용하는 경우 break superOuter을 대신 사용하십시오.