我正在创建一个概率结果模拟器程序。该程序读取某个.csv文件并预测每个游戏的结果。我遇到了6个相同的错误:错误:非法字符:' \ u2013'
这是我的代码:
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");
FileChooser.ExtensionFilter extFilter =
new FileChooser.ExtensionFilter("TEXT files (*.csv)", "*.CSV", ".xlsv", ".XLSV");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(stage);
run(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);
input.nextLine();
sortFile(file, input);
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)
{
String strList = Arrays.toString(input.nextLine().split("\t"));
String[] arrList = strList.split(",");
int homeRank = Integer.parseInt(arrList[1]);
System.out.println(homeRank);
int roadRank = Integer.parseInt(arrList[6]);
Random r = new Random();
int lowestTeamRank = Math.abs(homeRank - roadRank);
if (homeRank < roadRank)
{
double numForHomeTeam = r.nextInt(lowestTeamRank) - r.nextInt(2) + (getLastGameOutcome(arrList[4])* r.nextInt(3)) – getWinPct(arrList[2], arrList[3]);
double numForRoadTeam = r.nextInt(roadRank) + r.nextInt(2) + getLastGameOutcome(arrList[9])* r.nextInt(3) – getWinPct(arrList[7], arrList[8]);
}
else if (homeRank > roadRank)
{
double numForHomeTeam = r.nextInt(homeRank) - r.nextInt(2) + getLastGameOutcome(arrList[4])* r.nextInt(3) – getWinPct(arrList[2], arrList[3]);
double numForRoadTeam = r.nextInt(lowestTeamRank) - r.nextInt(2) + getLastGameOutcome(arrList[9])* r.nextInt(3) – getWinPct(arrList[7], arrList[8]);
}
else
{
double numForHomeTeam = r.nextInt(homeRank) - r.nextInt(2) + getLastGameOutcome(arrList[4])* r.nextInt(3) – getWinPct(arrList[2], arrList[3]);
double numForRoadTeam = r.nextInt(lowestTeamRank) - r.nextInt(2) + getLastGameOutcome(arrList[9])* r.nextInt(3) – getWinPct(arrList[7], arrList[8]);
}
return null;
}
public int getLastGameOutcome(String lastGame)
{
if (lastGame.charAt(0) == 'W')
{
return (int)(Math.random() * 3);
}
else
{
return (int)(Math.random() * -3);
}
}
public double getWinPct(String wins, String losses)
{
double wins = Double.parseDouble(wins);
double losses = Double.parseDouble(losses);
return wins / (wins + losses);
}
}
错误发生在sortFile方法中,其中if / else if / else语句是(numForHomeTeam和numForRoadTeam的公式)。错误发生在每个公式的末尾,其中getWinPct()从其他所有内容中减去。导致此错误的原因是什么?如何解决?
答案 0 :(得分:3)
字符\u2013
是一个短划线,而不是常规短划线(ascii 45)。在读入文件时,可能需要过滤非标准字符。在阅读.doc或其他类型的文件时,我不得不过滤掉一些不规则的字符
答案 1 :(得分:1)
我发现了为什么遇到错误。原来是因为我使用了从Word文档( - )复制和粘贴的否定符号,而不是在IDE中手动编写减法符号( - )。
答案 2 :(得分:0)
您的CSV中最有可能包含此字符的值。我建议你修复文件,使这些列只包含你期望的数字。
或者,您可以忽略任何不属于数字的字符,但这假设您知道为损坏的文件执行此操作是安全的。