-1
我正在將我的簡單聯繫人應用程序從正常的JAVA FX重寫爲FXML.THe問題在Lambda表達式中。我有接口,如:Java中的FXML和lambda表達式無法正常工作?
interface ObjectCreator {
void create(String[] array);
}
而問題是,當我用這個CSVFileReader:
public static class CSVFileReader {
String fileName;
CSVFileReader(String fileName) {
this.fileName = fileName;
}
void read(ObjectCreator creator) {
try {
BufferedReader csv = new BufferedReader(new FileReader(fileName));
String row = csv.readLine();
while (row != null) {
creator.create(row.split(","));
row = csv.readLine();
}
csv.close();
} catch (IOException e) {
}
}
}
在這段代碼中我使用lambda表達式來從文件中讀取聯繫人:
@FXML
public void openFile() {
FileChooser fileChooser = new FileChooser();
File fileE = fileChooser.showOpenDialog(scene.getWindow());
if (fileE != null) {
CSVFileReader reader = new CSVFileReader(fileE.getName());
data.clear();
reader.read(v ->
data.add(new Contact(v[0], v[1], v[2], v[3])));
}
System.out.println("open list");
}
我使用JDK 1.8.65如此新的一個。問題在於沒有錯誤或異常就像代碼「v-> data.add(new Contact(v [0],v [1],v [2],v [3]))」不會被調用。 問題是我做錯了什麼或它只是不會工作?
您需要更具體地瞭解您所面臨的問題(是什麼讓您認爲它不能工作*?)。你得到編譯錯誤,或運行時異常,或可能是不正確的結果? – Pshemo
對不起,你是對的問題是,沒有結果正在從文件中讀取,如3條記錄。並且沒有錯誤或異常就像代碼「v - > data.add(new Contact(v [0],v [1],v [2],v [3]))」不會被調用。 –