title: How can I restructure my highLow method to not throw an AIOOBE exception?
body: <p>I keep getting thrown an AIOOBE (ArrayIndexOutOfBoundsException) and have tried to no avail to work my way around it. If anyone has any input on how I might restructure my method or loops within it to allow for the variable "country" to hold on to it's assigned values and be returned at the end of the method, I would be extremely grateful. Thanks in advance!</p>
  
  <pre><code>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&lt;rows;j++){
              for(int i=1;i&lt;=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 &lt;= 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 &lt; a.length; i++) {
          min = i;
          for (int j = i + 1; j &lt; a.length; j++) {
              if (a[min] &gt; a[j]) {
                  min = j;
              }
          }
          if (min != i) {
              double temp = a[min];
              a[min] = a[i];
              a[i] = temp;
          }
      }
  return a;
  }
  </code></pre>
  
  <p>}</p>