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

时间:2015-10-17 01:54:15

标签: java string for-loop nested substring

您好我得到了一个文本文件A2Q2.txt。我的程序假设是读取文件,每行只打印40个字符,包括空格。打印40个字符后,程序应开始在第二行打印其他40个字符,依此类推。阅读文件很简单,但我仍然坚持如何使每行只打印40个字符。我的代码如下:

以下是示例段落:

在古代手稿中,将句子分成段落的另一种方法是换行符(换行符),然后是下一段开头的首字母。首字母是一个超大的大写字母,有时会超出文本边界。例如,可以在Beowulf的原始古英语手稿中看到这种风格。虽然不常见,但英语排版中仍然使用了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());
        }
    }
}

1 个答案:

答案 0 :(得分: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个字符。