文本对齐未正确格式化

时间:2018-01-04 18:36:10

标签: java formatting dynamic-programming

我试图创建一个程序来格式化输入的文本,这样每行都会被设置为特定的长度,并且无法通过例如20然后在每一行上都有相应的字符格式,并且有#34;。"填补空白以弥补设定的长度。

这是我到目前为止的输出:

 This.is..an..example
 of..text..that..will
 have..straight..left
 and.right....margins
 after formatting ...

由于某种原因"。"不会出现在格式之后以及格式之后和之后#34; g"一个点缺少一个空间取而代之。它似乎总是发生在最后一行。

这就是输出的样子:

 This.is..an..example
 of..text..that..will
 have..straight..left
 and.right....margins
 after.formatting....

代码:

import java.util.*;
public class FormattedPadding {
public static ArrayList<String> fullJustify(ArrayList<String> a, int b) {

    ArrayList<String> result = new ArrayList<String>();
    if(a == null || a.size() == 0)
        return result;
    int i = 0;
    int currentLength = 0;
    String temp = "";
    for(i = 0; i < a.size(); i++){
        currentLength += a.get(i).length() + 1;
        if(currentLength > b + 1) {
            result.add(temp);
            temp = "";
            currentLength = 0;
            i--;
            //System.out.println("Intermediate result: " + result);
        }
        else
            temp += a.get(i) + " ";
    }
    if(!temp.equals(""))
        result.add(temp);
    for(i = 0; i < result.size() - 1; i++){
        temp = result.get(i);
        String[] tempArray = temp.split(" ");
        int totalLength  = 0;
        for(int j =0; j < tempArray.length; j++)
            totalLength += tempArray[j].length();

        int[] spaceCount = getSpaceCount(b-totalLength, tempArray.length);
        for(int l =0; l < spaceCount.length; l++)
            System.out.print(spaceCount[l] + " " );
        System.out.println();
        temp = "";

        for(int j = 0; j < tempArray.length; j++){
            temp += tempArray[j];
            for(int k = 0; k < spaceCount[j]; k++)
                temp += ".";
        }
        result.set(i, temp);

    }

    temp = result.get(result.size() - 1);
    if(temp.length() < b){
        while(temp.length() < b)
            temp += ".";
    }
    else if(temp.length() > b)
        temp = temp.substring(0, b);
    result.set(result.size() - 1, temp);
    return result;          
}

public static int[] getSpaceCount(int freeSpace, int numOfStrings) {
    int size = numOfStrings - 1;
    int[] ret = new int[size + 1];
    if(size == 0){
        ret[0] = freeSpace;
    }
    else {
        for(int i =0; i < ret.length; i++) {
            if(size != 0){
                ret[i] = freeSpace % size == 0 ? freeSpace/size : freeSpace/(size + 1);
            }
            freeSpace = freeSpace - ret[i];
            size--;
        }
    }
    return ret;
}

public static void main(String[] args) {
    //ArrayList<String> a = new ArrayList<String>();

    System.out.println("#Enter ");
    String usrInput = BIO.getString();
    String[] items = usrInput.split("\\s+"); // Split where whitespace is encounterd using the RegEx
    ArrayList<String> newList = new ArrayList<String>(Arrays.asList(items));
    // ^Split input into ArrayList

    int b = 20; // Line length
    ArrayList<String> result = fullJustify(newList, b);

    for(int i =0; i < result.size(); i++) {
        System.out.println(result.get(i));
    }

    System.out.println(result);

}
}

1 个答案:

答案 0 :(得分:1)

首先,我认为如果任何单词的长度超过&#34; b&#34;

,您的代码将进入无限循环

关于此问题,您为最后一行创建了不同的逻辑

temp = result.get(result.size() - 1);
if(temp.length() < b){
    while(temp.length() < b)
        temp += ".";
}
else if(temp.length() > b)
    temp = temp.substring(0, b);
result.set(result.size() - 1, temp);

你可以远程控制这些部分,并将结果从result.size() - 1更改为result.size(),因此它将覆盖所有行:

for(i = 0; i < result.size(); i++){