Java 使用ArrayList写入文本文件

Java 使用ArrayList写入文本文件,java,Java,该程序基本上是从文本文件中读取数据,将当前数据存储到ArrayList中,然后(从用户输入)将数据写入同一文本文件。请让我知道我在这个子部分哪里出错了?文本文件中的数据如下所示: abc t1 1900 xyz t2 1700 编译器在output.format(“%s%s%s%n”, 公共类测试{ 专用扫描仪输入; 专用格式化程序输出; private ArrayList tk=new ArrayList(); 公共静态void main(字符串[]args){ justTesting应用程序

该程序基本上是从文本文件中读取数据,将当前数据存储到ArrayList中,然后(从用户输入)将数据写入同一文本文件。请让我知道我在这个子部分哪里出错了?文本文件中的数据如下所示:

abc t1 1900

xyz t2 1700

编译器在
output.format(“%s%s%s%n”,

公共类测试{
专用扫描仪输入;
专用格式化程序输出;
private ArrayList tk=new ArrayList();
公共静态void main(字符串[]args){
justTesting应用程序=新的justTesting();
app.create();
app.writeToFile();
}
公共void create(){
文本输入=新文本();
扫描仪输入=新扫描仪(System.in);
System.out.printf(“%s\n”,“请输入您的姓名、ID和年份:”);
while(input.hasNext()){
试一试{
entry.setName(input.next());
entry.setTelNumber(input.next());
entry.setDOB(input.next());
对于(int i=0;i
我删除了%n,它成功了。谢谢大家。

什么是
输出
声明为?它甚至声明/实例化了吗?我看不到这段代码。错误是怎么说的?你以
%s%s%n
格式声明了4个引用,你只传递了3个。输出被声明为
私有格式化程序输出
一个d错误显示线程“main”java.lang.NullPointerException中的
异常
@m0skit0该
%n
用于文本文件中的回车符。其工作原理类似于
/r/n
public class justTesting {
    private Scanner input;
    private Formatter output;
    private ArrayList<Student> tk = new ArrayList<Student>();

    public static void main(String[] args) {
        justTesting app = new justTesting();

        app.create();
        app.writeToFile();
    }

    public void create() {
        Text entry = new Text();
        Scanner input = new Scanner(System.in);

        System.out.printf("%s\n", "Please enter your name, ID, and year: ");

        while (input.hasNext()) {
            try {
                entry.setName(input.next());
                entry.setTelNumber(input.next());
                entry.setDOB(input.next());

                for (int i = 0; i < tk.size(); i++) {
                    output.format("%s%s%s%n", tk.get(i).getName(), tk.get(i)
                            .getTelNumber(), tk.get(i).getDOB());
                }

            } catch (FormatterClosedException fce) {
                System.err.println("Error writing to file.");
                return;
            } catch (NoSuchElementException nsee) {
                System.err.println("Invalid input. Try again: ");
                input.nextLine();
            }
            System.out.printf("%s\n", "Please enter your name, ID, and year: ");
        }
    }

    public void writeToFile() {
        try {
            output = new Formatter("testing.txt");
        } catch (SecurityException se) {
            System.err
                    .println("You do not have write access permission to this file.");
            System.exit(1);
        } catch (FileNotFoundException fnfe) {
            System.err.println("Error opening or creating file.");
            System.exit(1);
        }
    }
}