错误:非法的Characher:'\ u00bf'

时间:2016-12-12 12:36:56

标签: java

所以我正在研究我的java编码类的最终项目,当我尝试编译测试文件时,我收到一个错误:非法字符:'\ u00bf'我试过搜索错误而我找不到太多帮助解决错误。
Error

/*
 * File: USCrimeClass.java

 * Author: Darren Pirtle Jr.
 * Date: Decemeber 12, 2016
 *
 */



 import java.util.File;
 import java.util.FileInputStream;
 import java.util.FileNotFoundException;
 import java.util.Scanner;

 public class TestUSCrime {

     static Scanner input = new Scanner(System.in);



   /**

   *

   * @param args

   */


   public static void main(String[] args) {

       long startTime = System.currentTimeMillis();

       long endTime = 0;

       System.out.println("********** Welcome to the US Crime Statistical Application ******************");

       if (args.length != 1) {

       System.out.println("Usage: \"java UserCrimeStatsApp Crime.csv");

           return;

       }



       USCrimeClass[] data = read(args[0]);


       String choice;

       while (true) {


       String menu = "\nEnter the number of the question you want answered. Enter ‘Q’ to quit the program :\n"

                   + "1. What were the percentages in population growth for each consecutive year from 1994 – 2013?\n"

                   + "2. What year was the Murder rate the highest?\n"

                   + "3. What year was the Murder rate the lowest?\n"

                   + "4. What year was the Robbery rate the highest?\n"

                   + "5. What year was the Robbery rate the lowest?\n"

                   + "6. What was the total percentage change in Motor Vehicle Theft between 1998 and 2012?\n"

                   + "7. What was the total percentage change in property crimes between 2011 and 2013?\n"

                   + "8. What was the total percentage change in murder crimes between 2012 and 2013?\n"

                   + "Q. Quit the program";

           System.out.println(menu);

           choice = getInput();

           System.out.println();
           switch (choice) {


           case "1":

           showPercentage(data);

               break;

           case "2":

               System.out.println("The Murder rate was highest in "

                       + murderRate(data));

               break;

           case "3":

               System.out.println("The Murder rate was lowest in "

                       + lowestMurderRate(data));

               break;

           case "4":

               System.out.println("The Robbery rate was highest in "

                       + highestRobberyRate(data));

               break;

           case "5":

                System.out.println("The Robbery rate was lowest in "

                    + lowestRobberyRate(data));

               break;

           case "6":

               System.out.println("Total Percentage change in motor vehicle theft between(1998-2012) is "

                       + String.format("%.4f", chnage1998_2012(data))

                               + "%");

               break;

           case "7":

               System.out.println("Total Percentage change in property crimes between 1994 and 1998 is "
                       + String.format("%.4f", chnage1994_1998(data))

                               + "%");

               break;

           case "8":

               System.out.println("Total Percentage change in murder crimes between 2012 and 2013 is "

                       + String.format("%.4f", chnage2012_2013(data))

                               + "%");

               break;

           case "Q":

               System.out.println("Thank you for trying the US Crimes Statistics Program.");

               endTime = System.currentTimeMillis();

               System.out.println("Elapsed time in seconds was: "
+ (endTime - startTime) / 1000);

               return;

           default:

               System.out.println("Error: Invalid choice selected!! Try again.\n");

               break;

           }

       }




   }


   /**

    * Display menu to user

    */
   static void displayMenu() {


   }


   public static String getInput() {

       String choice;

       System.out.print("\nEnter your selection: ");

       choice = input.next();

       return choice;

   }



   /**

    * Return the highest murder rate for a year

    *

    * @param data

    * @return year with highest murder rate

    */


   public static int murderRate(USCrimeClass[] data) {

       int year = 0;

       float maxRate = 0;

       for (USCrimeClass crime : data) {

           if (crime.getMurderRate() > maxRate) {

               maxRate = crime.getMurderRate();

               year = crime.getYear();

           }

       }

       return year;

   }



   /**

    * Return the lowest murder rate for a year

    *

    * @param data

    * @return year with lowest murder rate

    */
   public static int lowestMurderRate(USCrimeClass[] data) {


       int year = 0;

       float minRate = data[0].getMurderRate();

       for (USCrimeClass crime : data) {

           if (crime.getMurderRate() < minRate) {

               minRate = crime.getMurderRate();

               year = crime.getYear();

           }

       }


       return year;

   }



   /***

    *

    * @param data

    * @return

    */


   public static int highestRobberyRate(USCrimeClass[] data) {

       int year = 0;

       float maxRate = 0;

       for (USCrimeClass crime : data) {

           if (crime.getRobberyRate() > maxRate) {

               maxRate = crime.getRobberyRate();

               year = crime.getYear();

          }

       }

       return year;

   }


   /****

    *

    * @param data

    * @return

    */


   public static int lowestRobberyRate(USCrimeClass[] data) {

       int year = 0;

       float minRate = data[0].getRobberyRate();

       for (USCrimeClass crime : data) {

           if (crime.getRobberyRate() < minRate) {

               minRate = crime.getRobberyRate();

               year = crime.getYear();

           }

       }


       return year;

   }



   /**

    * Calculate the percentage change in motor vehicle theft between

    * 1998-2012

    *

    * @param data

    * @return

     */


   static float chnage1998_2012(USCrimeClass[] data) {

       float change;

       int motorVehicleTheftIn1998 = data[4].getMotorVehicleTheft();

       int motorVehicleTheftIn2012 = data[18].getMotorVehicleTheft();

       change = (float) (motorVehicleTheftIn2012 - motorVehicleTheftIn1998)* 100 / motorVehicleTheftIn1998;

       return change;

   }



   /***

    *

    * @param data

    * @return

    */


   public static float chnage2012_2013(USCrimeClass[] data) {

       float change;

       float rate2012 = data[18].getMurderRate();

       float rate2013 = data[19].getMurderRate();

       change = (rate2013 - rate2012) * 100 / rate2012;

       return change;

   }



   /***

    *

    * @param data

    * @return

    */


   public static float chnage1994_1998(USCrimeClass[] data) {

       float change;

       int rate1994 = data[2].getPropertyCrime();

       int rate1998 = data[6].getPropertyCrime();

       change = (float) (rate1998 - rate1994) * 100 / rate1994;

       return change;

   }



   /**

    * Display the percentage population for each consecutive year

    *

    * @param data
    */


   static void showPercentage(USCrimeClass[] data) {

       float growth;

       for (int i = 0; i < data.length - 1; i++) {

           growth = 100 * (float) (data[i + 1].getPopulation() - data[i]

                           .getPopulation()) / data[i].getPopulation();

           System.out.println("[" + data[i].getYear() + "-"
                   + data[i + 1].getYear() + "]: "
                   + String.format("%.4f", growth) + "%");
      }
   }

   /**
    * function handles the choice given by the user
    * 
    * @param data
    */
   static void handleMenu(USCrimeClass[] data) {

   }

   /***
    * Read file data
    * 
    * @param filename
    * @return
    */
   public static USCrimeClass[] read(String filename) {
       USCrimeClass[] stats = new USCrimeClass[20];
       int count = 0;
       String line;
       try {
            // read file
           Scanner inputReader = new Scanner(new File(filename));

           // ignore column name
           inputReader.nextLine();
           while (inputReader.hasNext()) {
               line = inputReader.nextLine();
               String[] data = line.split(",");
               stats[count] = new USCrimeClass(Integer.parseInt(data[0]));
               stats[count].setPopulation(Integer.parseInt(data[1]));
               stats[count].setMurderRate(Float.parseFloat(data[5]));
               stats[count].setMotorVehicleTheft(Integer.parseInt(data[18]));
               stats[count].setPropertyCrime(Integer.parseInt(data[12]));

               stats[count].setRobberyRate(Float.parseFloat(data[9]));

               count++;

           }

       }
catch (FileNotFoundException e) {

           System.out.println(e);


       }


       return stats;

   }

}

enter code here

1 个答案:

答案 0 :(得分:3)

您似乎已将源代码文件保存为带有BOM(字节顺序标记)的UTF-16编码文档。 您可以尝试将其保存为UTF-8并使用-encoding utf8

进行编译

编辑:我可能错了。可能是您已经在使用UTF-8(https://en.wikipedia.org/wiki/Byte_order_mark),在这种情况下,只需将-encoding utf8添加到javac命令就可以了,或者将源文件保存为utf-8而不用bom