2015-10-17 55 views
-1

嗨,我給出了一個文本文件A2Q2.txt。我的程序假設讀取文件,並且每行僅打印40個字符,包括空格。打印40個字符後,程序應該在第二行開始打印另外40個字符,依此類推。讀取文件很容易,但我堅持如何讓它只能打印每行40個字符。使用嵌套for循環提取子字符串

下面是一個簡單的段落:

在古代手稿,另一種方式來劃分句子到段落是一個換行符(新行),然後被初始在下一段落的開頭我下面的代碼給出。首字母是超大寫字母,有時超出文本邊界。例如,這種風格可以在貝奧武甫的原始古英語手稿中看到。雖然不常見,但仍然在英文版中使用Outdenting [4]。現代英語版式通常通過縮進第一行來表示新的段落。這種風格可以從1787年的(手寫)美國憲法中看到。對於其他裝飾,可以在段間空格中添加常春藤葉或其他符號,或放入縮進空間。

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class A2Q2 { 
    public static final int MAX_CHARS_PER_LINE = 40; 

    public static void main(String[] args) { 
     printFile(); 
    } 

    public static void printFile() { 
     BufferedReader fileIn; 
     FileReader fileReaderIn; 
     String word; 
     String inputLine; 

     try { 
      fileReaderIn = new FileReader("A2Q2in.txt"); 
      fileIn = new BufferedReader(fileReaderIn); 
      inputLine =fileIn.readLine(); 

      while (inputLine != null) { 
       System.out.print(inputLine + " "); 
       inputLine = fileIn.readLine(); 
      } 
      fileIn.close(); 
     } 
     catch (IOException ioe) { 
      System.out.println(ioe.getMessage()); 
     } 
    } 
} 
+0

你看過[substring](http://www.tutorialspoint.com/java/java_string_substring.htm)嗎? – sam

+0

在這一行中,System.out.print(inputLine +「」);試試這個:System.out.print(inputLine.substring(0,40));看看會發生什麼。 –

+0

它給出了一個錯誤信息,指出字符串索引輸出或範圍:40 –

回答

0

請嘗試以下解決方案。它逐行讀取段落,然後對每行進行處理,以便從0插入第40個索引處的新行字符或插入最後一個新行字符。另外,不是在每一行上執行操作,它可以在整個段落中完成,方法是首先將其附加到構建器對象中並執行插入操作,但對於包含大量數據的文件可能不好。
由於您尚未提及有關包含多個段落的文件的任何內容,因此下面的解決方案最適合單個段落。


import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class A2Q2 { 

    public static final int MAX_CHARS_PER_LINE = 40; 

    public static void main(String[] args) { 
     printFile(); 
    } 

    public static void printFile() { 

     BufferedReader fileIn = null; 
     FileReader fileReaderIn = null; 
     String inputLine; 
     int lengthOfLine; 
     int newLineCount; 
     StringBuilder sb = new StringBuilder(); 

     try { 
      fileReaderIn = new FileReader("A2Q2in.txt"); 
      fileIn = new BufferedReader(fileReaderIn); 

      // read paragraph line by line 
      while ((inputLine = fileIn.readLine()) != null) { 

       // counter for iteration, set on each iteration for new line from paragraph 
       int ctr = 0; 
       // append line to string builder object 
       sb.append(inputLine); 
       lengthOfLine = sb.length(); 

       // identifies how many newline characters to be added in the line 
       newLineCount = lengthOfLine/MAX_CHARS_PER_LINE; 

       // if newCount > 0 is true, signifies that line needs to be broken to 40 chars 
       if(newLineCount > 0) { 

        while(ctr < newLineCount) { 
         // insert newline character at 40th index from last newline character 
         sb.insert((MAX_CHARS_PER_LINE+(MAX_CHARS_PER_LINE*ctr)+ctr), "\n"); 
         ctr++; 
        } 
        System.out.print(sb.substring(0, (MAX_CHARS_PER_LINE*newLineCount)+ctr)); 

        // delete the printed string and keeping the left over to be appended by next line 
        sb.delete(0, ((MAX_CHARS_PER_LINE*newLineCount)+ctr)); 
       } 
      } 
      // print the remaining string left in the builder object. 
      System.out.print(sb.toString()); 
     } 
     catch (IOException ioe) { 
      System.out.println(ioe.getMessage()); 
     } 
     finally { 

      if(fileIn != null) { 
       try { 
        fileIn.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      if(fileReaderIn != null) { 
       try { 
        fileReaderIn.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

對於上述代碼的輸入是:

In ancient manuscripts, another means to divide sentences in into paragraphs was a line break 
followed by an initial at the beginning of the next paragraph. An initial is an oversize capital 
letter, sometimes outdented beyond the margin of text. This style can be seen, for example, in 
the original Old English manuscript of Beowulf. Outdenting is still used in English typography, 
though not commonly.[4] Modern English typography usually indicates a new paragraph by indenting 
the first line. This style can be seen in the (handwritten) United States Constitution from 1787. 
For additional ornamentation, a hedera leaf or other symbol can be added to the inter-paragraph 
whitespace, or put in the indentation space. 

而基於(由於缺乏要求的)假設的輸出是:

In ancient manuscripts, another means to 
divide sentences in into paragraphs was 
a line break followed by an initial at 
the beginning of the next paragraph. An 
initial is an oversize capital letter, s 
ometimes outdented beyond the margin of 
text. This style can be seen, for exampl 
e, in the original Old English manuscrip 
t of Beowulf. Outdenting is still used i 
n English typography, though not commonl 
y.[4] Modern English typography usually 
indicates a new paragraph by indenting t 
he first line. This style can be seen in 
the (handwritten) United States Constit 
ution from 1787. For additional ornament 
ation, a hedera leaf or other symbol can 
be added to the inter-paragraph whitesp 
ace, or put in the indentation space. 

每行只有40個字符。

+0

謝謝你的回答真的幫了很多:) –

+0

歡迎你:) –