gpt4 book ai didi

java - 字符串缓冲区 : Adding a newline after a certain amount of words for formatting

转载 作者:行者123 更新时间:2023-12-01 06:04:08 25 4
gpt4 key购买 nike

好吧,我的问题是格式化程序的输出。我的程序是一个 madlib,它读取一个文件,然后允许用户输入名词、形容词、复数等,然后用用户输入的更新版本打印 madlib。这是我的文本文件:

小说中形容词最多的人物之一被称为“复数名词的泰山”。泰山是由一个/一个名词抚养长大的,生活在最黑暗地方中心的形容词丛林中。他大部分时间都在吃复数名词,并从一棵树荡到另一个名词。每当他生气时,他就会拍打自己的胸口并说:“有趣的声音!”这是他的 war 口号。泰山总是穿着由名词的皮肤制成的形容词短裤,他最好的 friend 是一只名为猎豹的形容词黑猩猩。他应该能够与大象和复数名词交谈。在电影中,泰山是按人名扮演的。

我在文件中扫描的标记是这些<>(我没有在上面的文本文件中显示它们,但是在它说形容词或名词或有趣的声音的地方,它真的是<形容词>,左箭头形容词和右箭头形容词),这就是用户输入的位置。我的程序中的所有内容都有效,除非我将其打印出来。它不是以上面的格式打印 madlib,而是以一长行打印出来。它不必匹配上面的格式,我只是希望它在长度为 50 之后打印换行符,这样更容易阅读。

这是我的代码:

import java.util.Map;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;

public class ReadFile
{
public static void main(String[] args) //throws Exception
{
Scanner s=new Scanner(System.in);
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String line;
StringBuffer storybuffer=new StringBuffer();

//Accept lines until next line null
while((line=br.readLine()) != null)
storybuffer.append(" "+line);

//Remove first space
storybuffer.delete(0, 1);
String story=storybuffer.toString();
//Split
StringTokenizer str=new StringTokenizer(story);
String word;
StringBuffer finalstory=new StringBuffer();

//Store added elements
Map<String,String> hash=new HashMap<String,String>();

while(str.hasMoreTokens())
{
word=str.nextToken();
if(word.contains("<"))
{
String add="";
//Element prompt could be more than one word
if(!word.contains(">"))
{
//Build multi-word prompt
String phrase="";
do{
phrase+=word+" ";
}while(!(word=str.nextToken()).contains(">"));
word=phrase+word;
}
//Account for element placeholder being immediately followed by . or , or whatever.
if(word.charAt(word.length()-1)!='>')
add=word.substring(word.lastIndexOf('>')+1);

//Store id of element in hash table
String id=word.substring(0,word.lastIndexOf('>')+1);
String value;

if(!hash.containsKey(id))
{
//New element
System.out.println("Enter a "+ id);
value=s.nextLine()+add;
hash.put(id, value);
}
//Previously entered element heres the problem for duplicates!
else
value=hash.get(id);
word=value;
}
finalstory.append(word+" ");
// if(finalstory.length() > 50){
// finalstory.append("\n");


}
System.out.println(finalstory.toString());
s.close();
}
}

有人知道如何解决这个问题吗?

最佳答案

import java.util.Map;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;

public class ReadFile {
public static void main(String[] args) //throws Exception
{
Scanner s = new Scanner(System.in);
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String line;
StringBuffer storybuffer = new StringBuffer();

//Accept lines until next line null
while ((line = br.readLine()) != null)
storybuffer.append(" " + line);

//Remove first space
storybuffer.delete(0, 1);
String story = storybuffer.toString();
//Split
StringTokenizer str = new StringTokenizer(story);
String word;
StringBuffer finalstory = new StringBuffer();

//Store added elements
Map < String, String > hash = new HashMap < String, String > ();

while (str.hasMoreTokens()) {
word = str.nextToken();
if (word.contains("<")) {
String add = "";
//Element prompt could be more than one word
if (!word.contains(">")) {
//Build multi-word prompt
String phrase = "";
do {
phrase += word + " ";
} while (!(word = str.nextToken()).contains(">"));
word = phrase + word;
}
//Account for element placeholder being immediately followed by . or , or whatever.
if (word.charAt(word.length() - 1) != '>')
add = word.substring(word.lastIndexOf('>') + 1);

//Store id of element in hash table
String id = word.substring(0, word.lastIndexOf('>') + 1);
String value;

if (!hash.containsKey(id)) {
//New element
System.out.println("Enter a " + id);
value = s.nextLine() + add;
hash.put(id, value);
}
//Previously entered element
else
value = hash.get(id);
word = value;
}
finalstory.append(word + " ");
// if(finalstory.length() > 50){
// finalstory.append("\n");


}
System.out.println(finalstory.toString());
s.close();
}
}

关于java - 字符串缓冲区 : Adding a newline after a certain amount of words for formatting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49163974/

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