gpt4 book ai didi

Java 程序是否仅为某些类型的 CSV 文件创建扫描仪?

转载 作者:行者123 更新时间:2023-11-30 10:53:12 25 4
gpt4 key购买 nike

我正在制作一个概率结果模拟器 Java 程序。该程序从 FileChooser 中的用户处获取 CSV 文件(或任何文档文件)。然后用户将点击运行按钮,程序将开始读取 CSV(我们的首选文件)文件。到目前为止,这是我的代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.stage.FileChooser;
import javafx.geometry.*;
import java.util.*;
import java.io.*;

public class POS extends Application
{
private Button runBtn = new Button("Run");
@Override
public void start(Stage stage)
{
GridPane pane = new GridPane();

VBox vBox = new VBox(20);
vBox.setPadding(new Insets(15));
Button selectBtn = new Button("Select File");
selectBtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
vBox.getChildren().add(selectBtn);

selectBtn.setOnAction(e->
{
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
File file = fileChooser.showOpenDialog(stage);

String regEx = "([^\\s]+(\\.(?i)(txt|doc|csv|pdf|xlsx))$)";

if (file.getName().matches(regEx))
{
run(file);
}

else
{
System.out.println("Please enter a valid CSV file");
}
});

RadioButton weekBtn = new RadioButton("Current Week");
RadioButton seasonBtn = new RadioButton("Entire Season");

runBtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");



seasonBtn.setDisable(true);
vBox.getChildren().add(weekBtn);
vBox.getChildren().add(seasonBtn);
vBox.getChildren().add(runBtn);

pane.add(vBox, 0, 0);
Scene scene = new Scene(pane, 500, 200);
stage.setScene(scene);
stage.setTitle("POS");
stage.show();
}
public void run(File file)
{
runBtn.setOnAction(e->
{
try
{
Scanner input = new Scanner(file);
sortFile(file, input);
input.nextLine();
input.close();
}

catch (InputMismatchException ex)
{
System.out.println("Error you seem to have typed the wrong type of file");
}
catch(IOException ex)
{
System.out.println("Error, file could not be found");
}


});
}
public ArrayList<String> sortFile(File file, Scanner input)
{
Random r = new Random();

input.next();
int homeRank = input.nextInt();
input.next();
input.next();
input.next();
input.next();
int roadRank = input.nextInt();
System.out.println("Home: " + homeRank + "road: " + roadRank);
int lowestTeamRank = Math.abs(homeRank - roadRank);

if (input.hasNext())
{
return null;
}
return null;
}

}

当我“用户”选择一个文件时,比如说,一个无效文件,程序会告诉我这是无效的。如果我选择一个名为“NFLData”的 .csv 文件,程序会告诉我“错误,您似乎输入了错误的文件类型”(MalformedException)。如果我选择一个没有任何内容的 .xlsx 文件,程序不会抛出格式错误的异常,而是告诉我它是空的。如何让我的程序接受我的 NFLData .csv 文件?

最佳答案

我意识到我的错误在运行 block 中。每当我启动 input.next() 时,扫描仪都会接收 .csv 中由逗号分隔符分隔的整行。

示例: jack ,22 岁,约翰,21 岁,亨利,24 岁

我创建了不正确的新引用类型,该引用指向 .csv 文件中的整行数据。

关于Java 程序是否仅为某些类型的 CSV 文件创建扫描仪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34100188/

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