使用嵌套 for 循环提取子字符串

Extracting substring using nested for loop

您好,我收到了一个文本文件 A2Q2.txt。我的程序假设读取文件并且每行只打印 40 个字符,包括 spaces。打印 40 个字符后,程序应开始在第二行打印其他 40 个字符,依此类推。读取文件很容易,但我不知道如何让它每行只打印 40 个字符。我的代码如下:

这是一个示例段落:

在古代手稿中,另一种将句子分成段落的方法是换行符(换行符),然后在下一段的开头加上一个首字母。首字母是一个超大的大写字母,有时会超出文本的边缘。例如,这种风格可以在贝奥武夫 (Beowulf) 的古英语原稿中看到。 Outdenting 仍在英文排版中使用,尽管不常见。 [4]现代英语排版通常通过缩进第一行来表示新段落。这种样式可以在 1787 年的(手写)美国宪法中看到。为了额外的装饰,可以在段间白色 space 上添加常春藤叶或其他符号,或者在缩进 space.

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 或插入的最后一个换行符开始的第 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 个字符。