存储字符串从一个文本文件和打印到屏幕 [英] Storing A String From A Text Document And Printing It To The Screen

查看:87
本文介绍了存储字符串从一个文本文件和打印到屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个code迄今:

So I have this code so far:

import java.util.*;
import java.io.*;

public class EmailMerge {

    public static void main(String[] args) throws IOException {

        Scanner templateReader = null;
        Scanner peopleReader = null;

        PrintWriter out = null;

        String template = "";
        ArrayList people = new ArrayList();

        try {

            templateReader = new Scanner(new FileInputStream("template.txt"));
            peopleReader = new Scanner(new FileInputStream("people.txt"));

            while(templateReader.hasNext()) {
                //System.out.print(templateReader.next());
                template = templateReader.next();
            }

            System.out.println(template);
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File(s) not found...");
            System.exit(0);
        }
    }
}

我有一个名为template.txt一个文本文件包含此:

I have a text document called template.txt that contains this:

Dear <<N>>,

Because you are <<A>> years old and <<G>>, we have a
free gift for you. You have absolutely nothing to buy;
just pay the shipping and handling charge of $9.99. To
claim your gift, call us immediately.
Thank you,

Office of Claims Department

和我有另一个文本文档称为people.txt包含像这样的人姓名和年龄:

and I have another text document called people.txt that contains names and ages of people like so:

John, 38, Male
Mary, 22, Female
so on and so forth...

我需要做的就是通过名单上所有的名字,并使用模板制作个性化的信息。然后,我必须给每个保存到一个独特的文本文档(即。John.txt)。我想要做的就是名单上的名字存储到一个ArrayList,我打电话的人,然后把模板存储到一个字符串,我把它叫做模板。

What I have to do is go through all the names on the list and make a personalized message using the template. Then I have to save each to a unique text document(i.e. John.txt). What I want to do is store the names on the list into an ArrayList, I called people, and then store the template into a string, which I called template.

不过,我的问题进来时,我尝试打印出模板。我不知道如果我将它存储错,如果有更好的方式来打印出来,但是当我用这个code我只是得到部门打印到屏幕上。我需要有存储在字符串模板中的整个事情,能够把它打印到屏幕在适当的格式,与它首先。
请帮帮忙,多谢!

However, my issue comes in when I try to print out the template. I can't tell if I am storing it wrong of if there is a better way to print it out, but when I use this code I just get "Department" printed to the screen. I need to have the whole thing stored in the String template and be able to print it to the screen in its proper format like it is above. Please Help, Thanks A Bunch!!

更新:
谢谢你们这么多的帮助!
还有一个问题。我终于在项目结束时,我已存储的所有必要信息到几个的ArrayList,但是当我尝试打印出来,将工作模板,但它确实是大约1000倍。下面是我使用的循环:

UPDATE: Thank you guys so much for the help! One more question. I am finally at the end of the project and I have stored all of the necessary information into a few ArrayLists, however when I try to print out the template it will work, but it does it about a 1000 times. Here is the loop I am using:

for(int j = 0; j < people.size(); j++){
    for(int i = 0; i < names.size(); i++){
        System.out.println(template.replace("<<N>>", names.get(i)).replace("<<A>>", ages.get(i)).replace("<<G>>", genders.get(i)));
    }
}

我存储所有的名字/年龄/性别的到相应的ArrayList。再次感谢!

I stored all of the names/ages/genders into the appropriate ArrayList. Thanks again!

推荐答案

您正在更换的模板的字符串变量每一次,而不是更新它(追加)。尝试下面的code:

You're replacing the template String variable each time instead of updating it (appending). Try the code below:

template += templateReader.next();

或者更好 - 在评论由Luiggi说 - 一个的StringBuilder

StringBuiler builder = new StringBuilder("");
while(templateReader.hasNext()) {
        //System.out.print(templateReader.next());
        builder.append(templateReader.next());
}
template = builder.toString();

的StringBuffer 提供了更好的效果 + 运营商,所以每当你追加字符串中一个循环 - 就像这个例子 - 它是更好地使用它。

StringBuffer offers better performance than the + operator, so whenever you append Strings in a loop - like in this example - it's better to use it.

这篇关于存储字符串从一个文本文件和打印到屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆