简体   繁体   English

Java错误:非法字符:“ \\ u2013”

[英]Java Error: illegal character: '\u2013'

I am creating a probabilistic outcome simulator program. 我正在创建一个概率结果模拟器程序。 This program reads a certain .csv file and predicts the outcome of each game. 该程序读取某个.csv文件并预测每个游戏的结果。 I am running into 6 of the same errors which is: Error: illegal character: '\–' 我遇到了6个相同的错误,即:错误:非法字符:'\\ u2013'

Here's my code: 这是我的代码:

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);
   }  

}

The error occurs in the sortFile method where the if/else if/ else statements are (The formulas for the numForHomeTeam and numForRoadTeam). 错误发生在if / else if / else语句所在的sortFile方法中(numForHomeTeam和numForRoadTeam的公式)。 The error is at the end of each formula where the getWinPct() is subtracted from everything else. 错误出现在每个公式的末尾,其中从其他所有内容中减去了getWinPct()。 What is causing this error and how do I fix it? 是什么导致此错误,我该如何解决?

The character \– is an en-dash, not the regular dash (ascii 45). 字符\–是一个破折号,而不是常规破折号(ASCII 45)。 You may need to filter for non-standard characters when you read in the file. 读取文件时,可能需要过滤非标准字符。 I've had to filter for a number of irregular characters when reading .doc or other types of files 阅读.doc或其他类型的文件时,我不得不过滤掉许多不规则字符

I have found out why I was encountering my error. 我发现了为什么遇到错误。 It turned out to be because I was using a negative symbol that was copied and pasted from a Word Document (-) instead of manually writing a subtraction symbol (-) in the IDE. 原来是因为我使用的是从Word文档(-)复制和粘贴的负号,而不是在IDE中手动编写减号(-)。

Most likely you have a value in your CSV which contains this character. 很可能您的CSV中有一个包含此字符的值。 I suggest you fix the file so these columns only contains the numbers you expect. 我建议您修复文件,以便这些列仅包含您期望的数字。

Alternatively you could ignore any characters which are not part of a number but this assumes you know it's safe to do this for a corrupt file. 或者,您可以忽略不属于数字的任何字符,但这假设您知道对损坏的文件执行此操作是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM