gpt4 book ai didi

Java,使用Scanner逐行获取数据

转载 作者:行者123 更新时间:2023-12-01 11:36:59 25 4
gpt4 key购买 nike

我基本上运行这段代码:

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/

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