gpt4 book ai didi

java - 文本对齐格式不正确

转载 作者:行者123 更新时间:2023-12-02 11:47:24 25 4
gpt4 key购买 nike

我正在尝试制作一个程序来格式化输入的文本,以便将每一行设置为特定的长度并且不能超过例如20,然后在每行上设置相应的字符格式并带有“.”填充间隙以弥补设定的长度。

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

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

由于某种原因,“.”没有出现在 after 和 format 之间,以及在“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);

}
}

最佳答案

首先,我认为如果任何单词的长度超过“b”,您的代码将进入无限循环

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

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++){

关于java - 文本对齐格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48101544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com