英文:
How to append a sentence with prefix and suffix in java?
问题
我正在尝试读取一个包含以下内容的输入文件:
input.txt
Hello world. Welcome,
to the java.
并且,我必须在句子前面附加前缀(BEGIN)和后缀(END),输出应该如下所示:
output expected:
BEGIN_Hello world_END.BEGIN_ Welcome,
to the java_END.
以下是我的输入文件读取函数。我正在读取整个文件并将其存储在数组列表中:
InputDetails.java
private List<String> readInput = new ArrayList<>();
public void readFile() throws IOException {
while((inputLine = input.readLine()) != null ) {
readInput.add(inputLine);
}
}
//Getter to return input file content
public List<String> getReadInput() {
return readInput;
}
以下是附加字符串与BEGIN和END的代码:
public void process() {
InputDetails inputD = new InputDetails();
for(int i=0; i < inputD.getReadInput().size(); i++) {
String sentence = inputD.getReadInput().get(i);
String splitSentence[] = sentence.split("\\.");
for(int j=0; j < splitSentence.length; j++) {
System.out.println(splitSentence[j]);
splitSentence[j] = "BEGIN_" + splitSentence[j] + "__END";
}
sentence = String.join(".", splitSentence);
inputD.writeToFile(sentence);
}
}
output getting:
BEGIN_SENTENCE__Hello world__END_SENTENCE.BEGIN_SENTENCE__Welcome
to the java.
**注意:**每个句子由一个句点(.)字符分隔。输出句子应以BEGIN_开头,并以__END结尾。句点字符不被视为句子的一部分。并且,输入文件由一个或多个空格分隔。只要句子中有句点(.),句子就是完整的,即使这意味着句子在新行上完成(就像我上面指定的输入一样)。所有特殊字符的位置应在输出中保留。句点(.)或逗号(,)与单词之间也可以有空格。例如:java . 或 Welcome ,。
有人可以帮我修复这个问题吗?谢谢。
英文:
I am trying to read an input file that contains the following:
input.txt
Hello world. Welcome,
to the java.
And, I have to append the sentence with prefix(BEGIN) and suffix(END) and the output should like the following:
output expected:
BEGIN_Hello world_END.BEGIN_ Welcome,
to the java_END.
Following is my input file reading function. I am reading an entire file and storing it in array list:
InputDetails.java
private List<String> readInput = new ArrayList<>();
public void readFile() throws IOException {
while((inputLine = input.readLine()) != null ) {
readInput.add(inputLine);
}
}
//Getter to return input file content
public List<String> getReadInput() {
return readInput;
}
And following is my code for appending the string with BEGIN and END:
public void process() {
InputDetails inputD = new InputDetails();
for(int i=0;i<inputD.getReadInput().size();i++) {
String sentence = inputD.getReadInput().get(i);
String splitSentence[] = sentence.split("\\.");
for(int j=0;j<splitSentence.length;j++) {
System.out.println(splitSentence[j]);
splitSentence[j] = "BEGIN_"+splitSentence[j]+"__END";
}
sentence = String.join(".",splitSentence);
inputD.writeToFile(sentence);
}
}
output getting:
BEGIN_SENTENCE__Hello world__END_SENTENCE.BEGIN_SENTENCE__Welcome
to the java.
Note: Each sentence is separated by a "." (period) character. The output Sentence should be prefixed with BEGIN_ and suffixed with __END. The period character is not considered a part of the sentence. And, input file are delimited by one or more spaces. The sentence is complete when it has period(.) Even if it means the sentence completes on the new line(just as the input that i specified above). All, the special chars position should be retained in the output. There can also be a space between period(.) or a comma(,) and a word. for eg: java . or Welcome ,
Can Anyone help me fix this? Thanks
答案1
得分: 1
首先,您需要将字符串列表连接成一个单一的字符串。然后,您可以使用String.split()
方法来根据.
字符拆分输入为多个部分。然后,您可以选择在该数组上运行循环,或者使用流方法(如下所示)来迭代您的句子。对于每个部分,只需将所需的BEGIN_
和_END
块附加到句子上。您可以使用+
运算符进行手动字符串连接,或者使用String.format()
的字符串模板(如下所示)。最后,重新引入用于拆分输入的.
分隔符,将这些部分连接成一个单一的字符串。
String fullString = String.join("", getReadInput());
Arrays.asList(fullString.split("\\.")).stream()
.map(s -> String.format("BEGIN_%s_END", s))
.collect(Collectors.joining("."));
英文:
First, you'll need to join your string list input into a single string. Then, you can use the String.split()
method to break up your input into parts delimited by the .
character. You can then choose to either run a loop on that array or use the stream method (as shown below) to iterate over your sentences. On each part, simply append the required BEGIN_
and _END
blocks to the sentence. You can use manual string concatenation using the +
operator or use a string template with String.format()
(as shown below). Finally, reintroduce the .
delimiter used to break the input by joining the parts back into a single string.
String fullString = String.join("", getReadInput());
Arrays.asList(fullString).split("\\.")).stream()
.map(s -> String.format("BEGIN_%s_END", s))
.collect(Collectors.joining("."));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论