Java-将字符串数组转换为长数组

时间:2018-10-14 15:09:03

标签: java arrays string long-integer

对于Java和数组仍然是新手。我在将数据从CSV读取到String数组并将其转换为只能处理数字的长数组时遇到困难。我这样做是为了最终可以尝试找到最大值/最小值,平均值等。在下面的第二张图像中,您可以看到我将数据从String数组打印到屏幕上。我的CSV有20列和21行。当我尝试解析和转换时,我的数组打印了部分数据,并用0填充了大多数索引。我试图找出是否将其转换为错误,或者它是否与筛选数组索引的for循环有关。

import java.io.FileReader;
import java.io.BufferedReader;
import java.util.Scanner;
import java.io.IOException;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.lang.Math;


public class USCrimes {
  int year;
  long population;
  long violentCrime;
  double violentCrimeRate;
  long murderManslaughter;
  double murderManslaughterRate;
  long rape;
  double rapeRate;
  long robbery;
  double robberyRate;
  long aggravatedAssault;
  double aggravatedAssaultRate;
  long propertyCrime;
  double propertyCrimeRate;
  long burglary;
  double burglaryRate;
  long larcenyTheft;
  double larcenyTheftRate;
  long motorVehicleTheft;
  double motorVehicleTheftRate;
  String fileName= "C:/Users/micha/OneDrive/Documents/College/SEP 18/Crime.csv";
  String crimeArray[];

    //getter and setter methods for variables between lines 17 - 36
    public void setYear(int year) {
      this.year = year;
    }

    public int getYear() {
      return year;
    }

    public long getViolentCrime() {
      return violentCrime;
    }

    public void setViolentCrime(long violentCrime) {
      this.violentCrime = violentCrime;
    }

    public double getViolentCrimeRate() {
      return violentCrimeRate;
    }

    public void setViolentCrimeRate(double violentCrimeRate) {
      this.violentCrimeRate = violentCrimeRate;
    }

    public long getMurderManslaughter() {
      return murderManslaughter;
    }

    public void setMurderManslaughter(long murderManslaughter) {
      this.murderManslaughter = murderManslaughter;
    }

    public double getMurderManslaughterRate() {
      return murderManslaughterRate;
    }

    public void setMurderRate(double murderManslaughterRate) {
      this.murderManslaughterRate = murderManslaughterRate;
    }

    public long getRape() {
      return rape;
    }

    public void setRape(Long rape) {
      this.rape = rape;
    }

    public double getRapeRate() {
      return rapeRate;
    }

    public void setRapeRate(double rapeRate) {
      this.rapeRate = rapeRate;
    }

    public long getRobbery() {
      return robbery;
    }

    public void setRobbery(long robbery) {
      this.robbery = robbery;
    }

    public double getRobberyRate() {
      return robberyRate;
    }

    public void setRobberyRate(float robberyRate) {
      this.robberyRate = robberyRate;
    }

    public long getAggravatedAssault() {
      return aggravatedAssault;
    }

    public void setAggravatedAssault(long aggravatedAssault) {
      this.aggravatedAssault = aggravatedAssault;
    }

    public double getAggravatedAssaultRate() {
      return aggravatedAssaultRate;
    }

    public void setAggravatedAssaultRate(double aggravatedAssaultRate) {
      this.aggravatedAssaultRate = aggravatedAssaultRate;
    }

    public long getPropertyCrime() {
      return propertyCrime;
    }

    public void setPropertyCrime(long propertyCrime) {
      this.propertyCrime = propertyCrime;
    }

    public double getPropertyCrimeRate() {
      return propertyCrimeRate;
    }

    public void setPropertyCrime(double propertyCrimeRate) {
      this.propertyCrimeRate = propertyCrimeRate;
    }

    public long getBurglary() {
      return burglary;
    }

    public void setBurglary(long burglary) {
      this.burglary = burglary;
    }

    public double getBurglaryRate() {
      return burglaryRate;
    }

    public void setBurglaryRate(double burglaryRate) {
      this.burglaryRate = burglaryRate;
    }

    public long getLarcernyTheft() {
      return larcenyTheft;
    }

    public void setLarcenyTheft(long larcenyTheft) {
      this.larcenyTheft = larcenyTheft;
    }

    public double getLarcernyTheftRate() {
      return larcenyTheftRate;
    }

    public void setLarcenyTheftRate(double larcenyTheftRate) {
      this.larcenyTheftRate = larcenyTheftRate;
    }

    public long getMotorVehicleTheft() {
      return motorVehicleTheft;
    }

    public void setMotorVehicleTheft(long motorVehicleTheft) {
      this.motorVehicleTheft = motorVehicleTheft;
    }

    public double getMotorVehicleTheftRate() {
      return motorVehicleTheftRate;
    }

    public void setMotorVehicleTheftRate(double motorVehicleTheftRate) {
      this.motorVehicleTheftRate = motorVehicleTheftRate;
    }

    public String getFileName() {
      return fileName;
    }

    public void setFileName(String fileName) {
      this.fileName = fileName;
    //end getter and setter methods for variables between lines 17 - 36

    }
}

  class TestUSCrime{
        public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          USCrimes newUSCrimes = new USCrimes();
          long startTime = System.currentTimeMillis();
          BufferedReader br = null;
          String csvComma = ",";
          String line = "";
          int index = 0;

          System.out.println("********** Welcome to the USCrime Statistical Application **************************\n");
          System.out.println("Which US Crime file would you like to use?\n");


          String userInput = input.nextLine();
          input.close();

          //printing the menu for user selection
          System.out.println("Please enter the number for the question you would like answered. At any time, enter Q to quit the program:\n");
          System.out.println("1. What were the percentages in population growth for each consecutive year from 1994 ñ 2013?");
          System.out.println("2. What year was the Murder rate the highest?");
          System.out.println("3. What year was the Murder rate the lowest?");
          System.out.println("4. What year was the Robbery rate the highest?");
          System.out.println("5. What year was the Robbery rate the lowest?");
          System.out.println("6. What was the total percentage change in MotorVehicle Theft between 1998 and 2012?");
          System.out.println("7. What year was the Violent Crime rate the highest?");
          System.out.println("8. What year was the Violent Crime rate the ");
          System.out.println("");
          //setting the filename for user defined input
          newUSCrimes.setFileName(userInput);

          try {
              br = new BufferedReader(new FileReader(newUSCrimes.fileName));
              while ((line = br.readLine()) != null) {
                  String [] crimesArray = line.split(csvComma);
                  long[] crimesArrayNumbers = new long[crimesArray.length];


                  System.out.println(crimesArray[0] + " , "+ crimesArray[1] + " , " + crimesArray[2] + " , " + crimesArray[3]);

                   for(int i = 0; i < crimesArrayNumbers.length;i++)
                   {
                       try
                       {
                           crimesArrayNumbers[index] = Long.parseLong(crimesArray[i]);
                           index++;}

                      catch (IndexOutOfBoundsException ioob){
                        }
                      catch (NumberFormatException e){
                      }
                  }//end for loop
                        crimesArrayNumbers = Arrays.copyOf(crimesArrayNumbers, index);
                        //System.out.println(Arrays.toString(crimesArrayNumbers));
            }//end try

              System.out.println();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
              try {
                  br.close();
              } catch (IOException e) {
                  e.printStackTrace();
                  }
                }
            }

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

          //calculating the elapsed time using milliseconds and subsequently dividing by 1000.
          long endTime = System.currentTimeMillis();;
          long timeElapsed = endTime - startTime;
          System.out.println("Elapsed time in seconds was: " + (timeElapsed / 1000));

          if (args.length != 1) {
              System.out.println("Usage: \"java TestUSCrime Crime.csv");
            return;
        } // end if
    } //end main
} //end TestUSCrimes

Output for the long[] crimeArrayNumbers. Most of my values are 0 Output for String [] crimeArray from the CSV data

1 个答案:

答案 0 :(得分:2)

您不会在主循环内重置索引变量。添加

index=0;

在此行之前

for(int i = 0; i < crimesArrayNumbers.length;i++)