簡體   English   中英

Java錯誤:非法字符:“ \\ u2013”

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

我正在創建一個概率結果模擬器程序。 該程序讀取某個.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()。 是什么導致此錯誤,我該如何解決?

字符\–是一個破折號,而不是常規破折號(ASCII 45)。 讀取文件時,可能需要過濾非標准字符。 閱讀.doc或其他類型的文件時,我不得不過濾掉許多不規則字符

我發現了為什么遇到錯誤。 原來是因為我使用的是從Word文檔(-)復制和粘貼的負號,而不是在IDE中手動編寫減號(-)。

很可能您的CSV中有一個包含此字符的值。 我建議您修復文件,以便這些列僅包含您期望的數字。

或者,您可以忽略不屬於數字的任何字符,但這假設您知道對損壞的文件執行此操作是安全的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM