Java错误:非法字符:'\u2013'

3
我将创建一个概率结果模拟程序。该程序读取特定的 .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()减去其他内容。是什么导致了这个错误,我该如何修复它?

\uxxxx 是一个十六进制常量。 - JBALI
3个回答

3

字符\u2013是一个en-dash,而不是常规短横线(ascii 45)。 当您读取文件时,您可能需要过滤非标准字符。当阅读.doc或其他类型的文件时,我必须过滤掉一些不规则字符。


IDE设置中的过滤器? - Bytes
1
这意味着它可能被错误地用作负号,因此不能忽略。+1 - Peter Lawrey

3

我找到了我遇到错误的原因。原来是因为我复制并粘贴了一个从Word文档中复制的负号(-),而不是在IDE中手动输入减号(-)。


0

很可能您在CSV中有一个包含此字符的值。 我建议您修复文件,以便这些列仅包含您期望的数字。

或者,您可以忽略任何不属于数字的字符,但这意味着您知道对于一个损坏的文件这样做是安全的。


我已经将我的.csv文件中的行转换为字符串数组,因此它们都是字符串。你认为可能是因为我调用了getLastGameOutcome()和getWinPct()方法,并传递了数组索引,所以才会出现这种情况吗?我应该将这些参数转换为整数吗? - Bytes
@Bytes 你是说这个简单的例子行无法解析吗?我很确定这不是导致错误的行。顺便问一下,为什么要用两种方式拆分同一行,而不是只拆分一次呢? - Peter Lawrey
我只在我的代码中查看这一行。另外,我进行了两次拆分,首先是根据任何空格(例如:Green Bay),逗号拆分是为了将每个值放置在自己的索引中。 - Bytes
@Bytes逗号+空格是在您解析两次行之后的分隔符。这是基于您示例的代码部分http://ideone.com/OadiFU - Peter Lawrey
1
嗯,那似乎更方便。我尝试通过擦除一个公式直到r.nextInt(3)来调试编程。所以减号和getWinPct()都被擦除了。错误消失了。 - Bytes
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接