【问题标题】:Java Error: illegal character: '\u2013'Java 错误:非法字符:'\u2013'
【发布时间】:2016-03-10 23:38:00
【问题描述】:

我正在创建一个概率结果模拟器程序。该程序读取某个 .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);
   }  

}

错误发生在 if/else if/else 语句所在的 sortFile 方法中(numForHomeTeam 和 numForRoadTeam 的公式)。错误出现在每个公式的末尾,其中 getWinPct() 从其他所有内容中减去。是什么导致了这个错误,我该如何解决?

【问题讨论】:

  • \uxxxx 是一个十六进制常量

标签: java arrays hex


【解决方案1】:

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

或者,您可以忽略不属于数字的任何字符,但这假设您知道对损坏的文件执行此操作是安全的。

【讨论】:

  • 我已将 .csv 中的行转换为字符串数组,因此它们实际上都是字符串。你认为这可能是因为我调用了我传入数组索引的 getLastGameOutcome() 和 getWinPct() 方法吗?我应该将这些参数转换为整数吗?
  • @Bytes 不,我认为您有一个包含此字符的文件,这就是我这么说的原因。您需要删除该字符或忽略它。并非所有错误都是编码错误,有时是错误数据。
  • 我的行是:Detroit, 25, 4, 7, W, Green Bay, 7, 7, 4, L [0] 索引是 Detroit。我不知道它的哪一部分可能是坏数据,会不会是行中的字母,例如:W 和 L?
  • @Bytes 你是说这个简单的示例行无法解析?我很确定这不是导致此错误的行。顺便说一句,为什么您将同一行拆分为两种方式,为什么不只拆分一次?
  • 嗯,这似乎更方便。我尝试通过删除其中一个公式直到 r.nextInt(3) 来调试编程。所以减号和 getWinPct() 被删除。错误消失了。
【解决方案2】:

字符\u2013 是一个破折号,而不是常规破折号(ascii 45)。读入文件时,您可能需要过滤非标准字符。在阅读 .doc 或其他类型的文件时,我不得不过滤掉一些不规则字符

【讨论】:

  • 在 IDE 设置中过滤?
  • 这意味着它可能被错误地使用而不是负号,所以它不能被忽略。 +1
【解决方案3】:

我找到了我遇到错误的原因。原来是因为我使用的是从 Word 文档 (-) 复制和粘贴的负号,而不是在 IDE 中手动编写减号 (-)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 2016-06-10
    • 1970-01-01
    • 2013-12-18
    • 2018-09-27
    相关资源
    最近更新 更多