StringBuffer:在格式化

时间:2018-03-08 01:32:04

标签: java printing io hashmap

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

小说中最具形容词的人物之一被称为"复数名词的泰山。"泰山由一个名词提出,生活在最黑暗的地方心中的形容词丛林中。他大部分时间都在吃复数名词并从树上摆动到名词。每当他生气的时候,他就会捶胸顿足,然后说,"有趣的噪音!"这是他的战争口号。泰山总是穿着由一个名词的皮肤制成的形容词短裤,他最好的朋友是一个名叫猎豹的形容词黑猩猩。他应该能够与大象和复数名词交谈。在电影中,泰山是由人的名字扮演的。

我在文件中扫描的令牌是这些<> (我没有在上面的文本文件中显示它们,但是它说的是形容词或名词或有趣的噪音,它的确是<形容词>左箭头形容词和右箭头形容词之间没有空格)而且那里有用户输入被放置。我的程序中的所有内容都有效,除非我将其打印出来。它不是以上面的格式打印出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();
    }
}

任何人都有任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:-1)

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();
  }
}