Java,使用Scanner逐行获取数据

标签 java

我基本上运行这段代码:

AddressBook.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package addressbook;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author hassan
 */
public class AddressBook extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
        new AddressBookMapper().parseDataFile();
    }
}

AddressBookMapper.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package addressbook;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
 *
 * @author hassan
 */
public class AddressBookMapper {
    private File dataFile;
    private FileWriter fileWriter;
    private PrintWriter printWriter;
    public ArrayList lines;

    private void openDataFile() {
        this.dataFile = new File("src/addressbook/datafile.txt");
    }
    public void writeData() {

        System.out.println(this.lines);
        Iterator iterator = this.lines.iterator();
        System.out.println(this.lines.get(0));
        while(iterator.hasNext()) {
            System.out.println();
            System.out.println("-----");
        }
    }
    public void createAddress(String fullName, String email, String telephoneNumber, String mobileNumber) {
        this.openDataFile();
        try {
            this.fileWriter = new FileWriter(this.dataFile, true);
            this.printWriter = new PrintWriter(this.fileWriter);
            this.printWriter.append(fullName + "," + email + "," + telephoneNumber + "," + mobileNumber + "\n");
            this.printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public int getLastId() {
        return 0;
    }
public void parseDataFile() {
    File dataFile = new File("src/addressbook/datafile.txt");

    try {
        Scanner scanner = new Scanner(dataFile, "UTF-8").useDelimiter("\\n");

        while(scanner.hasNext()) {
            System.out.println(scanner.next());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    this.writeData();
}
    private int findLastAddressId() {
        return 0;
    }
}

我的数据文件文本:

ff,ff,fff,ff
krfr,frffr,frfs,ff
a,a,b,c
d,a,f,e
fa,e,f,a

但是,由于某种原因,我得到了这个输出:

ff,ff,fff,ff
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
krfr,frffr,frfs,ff
a,a,b,c
d,a,f,e
fa,e,f,a
null
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
    at addressbook.AddressBookMapper.writeData(AddressBookMapper.java:36)
    at addressbook.AddressBookMapper.parseDataFile(AddressBookMapper.java:69)
    at addressbook.AddressBook.main(AddressBook.java:36)
    ... 11 more
Exception running application addressbook.AddressBook
Java Result: 1

有什么解决办法吗?我基本上试图从数据文件中逐行获取文本并将其堆叠到我的 ArrayList 中。另外,在写入数据文件时,会在文档末尾添加一个空行。

最佳答案

你能尝试一下吗-

while(scanner.hasNext()) {
    lines.add(scanner.next());
}

关于Java,使用Scanner逐行获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869065/

相关文章:

java - 如何从计算机或从 URL 加载文件

java - 所有 JUnit 测试运行时的 Eclipse 通知

java - 键盘输入带有重音字符 : Java

java:如何为变量自动生成自定义方法

java - 我应该使用lock.lock() : in this method?

java - EL1008E : Property or field 'content' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?

java - 字段 @Inject 在 Dagger2 中不起作用

java - 在 Spring 中使用哪种传播?

java - 使用带有 Groovy 2.6+ 的 Spock 框架来支持 Java 8+ 语法

Java HttpURLConnection如何接收来自服务器的多个响应