java - 如何改进我的 java 代码以使其动态化?如果消息块是两位数或三位数
问题描述
给定一个字符限制将消息拆分为有意义的块而不拆分单词。
import java.util.ArrayList;
public class splitMessage {
static ArrayList<String> splitText(String message, int charLimit) {
return splitTextAuxUsingSplit(message, charLimit);
}
static ArrayList<String> splitTextAuxUsingSplit(String message, int charLimitOriginal) {
// Decrease the char limit to accomodate chunk number at the end i.e. (1/3). For
// **now assuming, the message chunks won't be more than 9** //what if //message chunks are in double or triple digit? how do I improvise?
int charLimit = charLimitOriginal - 7;
// New arraylist to store message chunks
ArrayList<String> result = new ArrayList<String>();
String[] splitted = message.split(" ");
String temp;
for (int i = 0; i < splitted.length - 1; i++) {
temp = splitted[i];
// while length of temp and the next element combined is less than charLimit,
// temp = temp + next element
// Last element to be taken care of after this loop
while ((temp + 1 + splitted[i + 1]).length() <= charLimit && i + 1 < splitted.length - 1) { // +1 for space
temp = temp + " " + splitted[i + 1];
i++;
}
result.add(temp);
}
// Take care of the last element
// Add the last element from splitted to the last element of result if their
// combined length is less than charLimit
String lastElement = result.get(result.size() - 1);
if (lastElement.length() + 1 + splitted[splitted.length - 1].length() < charLimit) { // +1 for space
result.set(result.size() - 1, lastElement + " " + splitted[splitted.length - 1]);
} else {
result.add(splitted[splitted.length - 1]);
}
// append message chunk number for ex (1/3)
int resultSize = result.size();
for (int i = 0; i < resultSize; i++) {
result.set(i, "[" + (i + 1) + "/" + resultSize + "]: " + result.get(i));
}
return result;
}
public static void main(String[] args) {
String message;
int charLmit;
message = "Hi Sivasrinivas, your Uber is arriving now! And this is a bigger text. Hi Sivasrinivas, your Uber is arriving now! And this is a bigger text. Hi Sivasrinivas, your Uber is arriving now! And this is a bigger text Hi Sivasrinivas, your Uber is arriving now! And this is a bigger text ";
charLmit = 25;
ArrayList<String> result = splitText(message, charLmit);
for (String item: result) {
System.out.println(item);
}
}
}
我如何即兴创作此代码,以便即使对于像 [1/300] 或 [29/35] 这样的 2,3 位消息块,它也能提供正确的输出
解决方案
这个任务没有一个非常有效的解决方案,你只需要在一个循环中尝试不同的块大小。您最初假设文本可以分成不超过 9 个块,因此信息文本将只有 7 个字符长,例如"[1/9]: "
. 您将文本实际拆分为长度块charLimit - 7
。如果在那之后你得到不超过 9 个块 - 一切都很好。如果不是,那么您需要假设文本可以分成不超过 99 个块。信息文本现在将有 9 个字符长,例如"[12/99]: "
。因此,您尝试将文本拆分为 length 块charlimit - 9
。如果你得到少于 100 个块 - 一切都很好。如果不是 - 您再次将最大值增加到 1000 并将信息文本大小增加 2 并重复该过程,直到找到解决方案。
import java.util.ArrayList;
import java.util.List;
public class SplitMessage {
static List<String> splitWordsToLimit(String[] words, int charLimit) {
List<String> result = new ArrayList<>();
StringBuilder line = new StringBuilder();
int wordsOnThisLine = 0;
for (String word : words) {
if (wordsOnThisLine == 0 ||
(line.length() + 1 + word.length() <= charLimit)) {
if (wordsOnThisLine != 0) {
line.append(" ");
}
line.append(word);
wordsOnThisLine++;
} else {
result.add(line.toString());
wordsOnThisLine = 1;
line = new StringBuilder(word);
}
}
if (wordsOnThisLine != 0) {
result.add(line.toString());
}
return result;
}
static List<String> splitText(String message, int charLimit) {
String[] words = message.split(" ");
int maxChunks = 10;
int infoSize = "[1/9]: ".length();
List<String> chunks;
while (true) {
chunks = splitWordsToLimit(words, charLimit - infoSize);
if (chunks.size() < maxChunks && charLimit > infoSize + 1) {
break;
}
maxChunks *= 10;
infoSize += 2;
}
int digits = String.valueOf(chunks.size()).length();
String infoFormat = "[%" + String.valueOf(digits) + "d/" +
String.valueOf(chunks.size()) + "]: ";
List<String> result = new ArrayList<>();
for (int i = 0; i < chunks.size(); i++) {
result.add(String.format(infoFormat, i + 1) + chunks.get(i));
}
return result;
}
public static void main(String[] args) {
String message;
int charLimit;
message = "Hi Sivasrinivas, your Uber is arriving now! And this is a bigger text. Hi Sivasrinivas, your Uber is arriving now! And this is a bigger text. Hi Sivasrinivas, your Uber is arriving now! And this is a bigger text Hi Sivasrinivas, your Uber is arriving now! And this is a bigger text ";
charLimit = 25;
List<String> result = splitText(message, charLimit);
for (String item: result) {
System.out.println(item);
}
}
}
输出charLimit = 50
:
[1/7]: Hi Sivasrinivas, your Uber is arriving now!
[2/7]: And this is a bigger text. Hi Sivasrinivas,
[3/7]: your Uber is arriving now! And this is a
[4/7]: bigger text. Hi Sivasrinivas, your Uber is
[5/7]: arriving now! And this is a bigger text Hi
[6/7]: Sivasrinivas, your Uber is arriving now!
[7/7]: And this is a bigger text
输出charLimit = 25
:
[ 1/20]: Hi Sivasrinivas,
[ 2/20]: your Uber is
[ 3/20]: arriving now!
[ 4/20]: And this is a
[ 5/20]: bigger text. Hi
[ 6/20]: Sivasrinivas,
[ 7/20]: your Uber is
[ 8/20]: arriving now!
[ 9/20]: And this is a
[10/20]: bigger text. Hi
[11/20]: Sivasrinivas,
[12/20]: your Uber is
[13/20]: arriving now!
[14/20]: And this is a
[15/20]: bigger text Hi
[16/20]: Sivasrinivas,
[17/20]: your Uber is
[18/20]: arriving now!
[19/20]: And this is a
[20/20]: bigger text
推荐阅读
- python - 我所有的 csv 数据都放在一行上,我怎么能把它放在不同的行上?
- flutter - 错误 image_picker: 类型 (null) 不支持压缩。以原始质量返回图像
- python - python最小化突然给出不同的结果
- php - 如何在表中的 JSON 数据中搜索特定数据
- spring-boot - Spring JPA 使用 @Query 返回两个 DB2 TIMESTAMP 值之间的数据
- python - 使用熊猫从未命名或空白的列中删除行
- elasticsearch - 如何验证失败的 ElasticSearch 恢复?
- nginx - Flask web:无法通过域名打开网站
- javascript - How to use conditional rendering inside map in React
- python - 预提交x VSCode:env:python3.9:没有这样的文件或目录