如何重构我的highLow方法以不抛出AIOOBE异常?

时间:2014-04-04 14:54:15

标签: java arrays exception exception-handling bufferedwriter

我一直被抛出一个AIOOBE(ArrayIndexOutOfBoundsException)并试图无法用它来解决它。如果有人对我如何重构我的方法或其中的循环有任何意见,以允许变量“country”保持它的指定值并在方法结束时返回,我将非常感激。提前谢谢!

public class CarbonAnalysis{
public static void main(String args[]) throws IOException{
    Scanner scan = new Scanner(System.in);
    String fileName;
    File file;
    Scanner in;

    do{
        //try{
            System.out.println("Please enter a valid name of a file to be sorted");
            fileName = scan.next();
            file=new File(fileName);
        //}catch(IOException e){
            //System.out.println("Please enter a valid filename");
            //fileName = scan.next();
            //file=new File(fileName);
        //}
        // I originally had a try/catch clause here because initially it actually caught an exception, but now at completion it's causing an error if left in.
        // That error being: "exception IOException is never thrown in body of corresponding try statement." so I've //'ed it out.
    }while(file.exists()!=true);

    in = new Scanner(new File(fileName));

    in.nextLine();
    int rows = in.nextInt();

    GetSortPack(rows, in);

    System.out.println("Output file CarbonDioxideAnalysis.txt has been created.");
}
public static void GetSortPack(int rows, Scanner in){
    String[] country = new String[rows];
    double[] totalCO2 = new double[rows];
    double[] roadCO2 = new double[rows];
    double[] CO2PerPerson = new double[rows];
    double[] carsPerPerson = new double[rows];

    while(in.hasNext()){
        for(int j=0;j<rows;j++){
            for(int i=1;i<=5;i++){
                if(i==1){
                    country[j] = in.next();
                }
                if(i==2){
                    totalCO2[j] = in.nextDouble();
                }
                if(i==3){
                    roadCO2[j] = in.nextDouble();
                }
                if(i==4){
                    CO2PerPerson[j] = in.nextDouble();
                }
                if(i==5){
                    carsPerPerson[j] = in.nextDouble();
                }
            }
        }
    }
    double[] sortedTotalCO2=SelectionSort(totalCO2);
    double[] sortedRoadCO2=SelectionSort(roadCO2);
    double[] sortedCO2PerPerson=SelectionSort(CO2PerPerson);
    double[] sortedCarsPerPerson=SelectionSort(carsPerPerson);

    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("CarbonDioxideAnalysis.txt"));
        for (int i = 1; i <= 4; i++) {
            if(i==1){
                out.write("The country with the lowest total emissions: " + highLow(country, totalCO2, sortedTotalCO2, 2));
                out.newLine();
                out.write("The country with the highest total emissions: " + highLow(country, totalCO2, sortedTotalCO2, 1));
                out.newLine();
                out.write("Canada is ranked " + highLow(country, totalCO2, sortedTotalCO2, 3) + " out of " + rows + " countries for total lowest emissions.");
                out.newLine();
                out.newLine();
            }
            if(i==2){
                out.write("The country with the lowest total road emissions: " + highLow(country, roadCO2, sortedRoadCO2, 2));
                out.newLine();
                out.write("The country with the highest total road emissions: " + highLow(country, roadCO2, sortedRoadCO2, 1));
                out.newLine();
                out.write("Canada is ranked " + highLow(country, roadCO2, sortedRoadCO2, 3) + " out of " + rows + " countries for total lowest road emissions.");
                out.newLine();
                out.newLine();
            }
            if(i==3){
                out.write("The country with the lowest per-person road emissions: " + highLow(country, CO2PerPerson, sortedCO2PerPerson, 2));
                out.newLine();
                out.write("The country with the highest per-person road emissions: " + highLow(country, CO2PerPerson, sortedCO2PerPerson, 1));
                out.newLine();
                out.write("Canada is ranked " + highLow(country, CO2PerPerson, sortedCO2PerPerson, 3) + " out of " + rows + " countries for lowest per-person road emissions.");
                out.newLine();
                out.newLine();
            }
            if(i==4){
                out.write("The country with the lowest total emissions: " + highLow(country, carsPerPerson, sortedCarsPerPerson, 2));
                out.newLine();
                out.write("The country with the highest total emissions: " + highLow(country, carsPerPerson, sortedCarsPerPerson, 1));
                out.newLine();
                out.write("Canada is ranked " + highLow(country, carsPerPerson, sortedCarsPerPerson, 3) + " out of " + rows + " countries for lowest number of cars per-thousand persons.");
                out.newLine();
                out.newLine();
            }
        }
        out.close();
    }catch (IOException e) {}
}
public static String highLow(String[] countries, double[] original, double[] sorted, int maxOrMin){
    int temp = 1;
    int canada = 0;
    int a = (original.length-1);
    String country = null;
        if(maxOrMin==1){
            temp=java.util.Arrays.asList(original).indexOf(sorted[0]);
            country = (countries[temp]);
        }else if(maxOrMin==2){
            temp=java.util.Arrays.asList(original).indexOf(sorted[a]);
            country = (countries[temp]);
        }else{
            canada=java.util.Arrays.asList(countries).indexOf("Canada");
            temp=java.util.Arrays.asList(original).indexOf(sorted[canada]);
            country = String.valueOf(temp);
        }
    return String.valueOf(country);
}
public static double[] SelectionSort(double[] b) {
    double[] a = new double[b.length];
    System.arraycopy(b, 0, a, 0, b.length);
    int min=-1;
    for (int i = 0; i < a.length; i++) {
        min = i;
        for (int j = i + 1; j < a.length; j++) {
            if (a[min] > a[j]) {
                min = j;
            }
        }
        if (min != i) {
            double temp = a[min];
            a[min] = a[i];
            a[i] = temp;
        }
    }
return a;
}

}

0 个答案:

没有答案