title: finding standard deviation giving me "java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10"
body: <p>my specific problem is where I try to calculate the Standard deviation. I looked up how to do this and found a code online that would work. when I implemented it and ran it I got the error.</p>
  <p>Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at classes2.Supermarkets.getStanderedDeviation(Supermarkets.java:73)
  at classes2.Supermarkets.displayStanderedDeviation(Supermarkets.java:89)
  at classes2.TestSupermarket.main(TestSupermarket.java:15)</p>
  <p>Im new to java and any and all help is greatly appreciated thank you.</p>
  <pre><code>package classes2;
  import java.text.NumberFormat;
  import java.util.Locale;
  
  public class Supermarkets {
      String names[];
      double profits[];
      double sum = 0, mean, n=10200000;
      NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
  
      public Supermarkets(String[] names, double[] profits) {
          super();
          this.names = names;
          this.profits = profits;
      }
  
      public void displayStatus() {
          System.out.println(&quot;Original cities profit&quot;);
          System.out.printf(&quot;%-20s%s\n&quot;, &quot;City&quot;, &quot;Profit&quot;);
          String profit;
          for (int i = 0; i &lt; names.length; i++) {
              profit = formatter.format(profits[i]);
              System.out.printf(&quot;%-20s%s\n&quot;, names[i], profit);
          }
          System.out.println();
      }
  
      public double getAverage() {
          Double totalProfit = 0.0;
          for (int i = 0; i &lt; names.length; i++) {
              totalProfit += profits[i];
          }
          totalProfit = totalProfit / names.length;
          return totalProfit;
      }
  
      public void displayAverage() {
          double average = getAverage();
          System.out.println(&quot;Average profit for the Supermarket chain is &quot; + formatter.format(average));
          System.out.println();
      }
  
      public void displayHighProfitCity() {
          double highProfit = 0.0;
          String highProfitCity = &quot;&quot;;
          for (int i = 0; i &lt; names.length; i++) {
              if (highProfit &lt; profits[i]) {
                  highProfit = profits[i];
                  highProfitCity = names[i];
              }
          }
          System.out.println(&quot;Highest profit city is &quot; + highProfitCity);
          System.out.println();
      }
  
      public void displayCityAboveAverageProfit() {
          double average = getAverage();
          System.out.println(&quot;Cities at or above average &quot;);
          System.out.printf(&quot;%-20s%s\n&quot;, &quot;City&quot;, &quot;Profit&quot;);
          String profit;
          for (int i = 0; i &lt; names.length; i++) {
              if (profits[i] &gt;= average) {
                  profit = formatter.format(profits[i]);
                  System.out.printf(&quot;%-20s%s\n&quot;, names[i], profit);
              }
          }
          System.out.println();
      }
  
      public double getStanderedDeviation(){
          for(int i=0;i&lt;n;i++)
          {
              sum=sum+profits[i];
          }
          mean=sum/n;
          System.out.println(&quot;Mean :&quot;+mean);
          sum=0;
          for(int i=0;i&lt;n;i++)
          {
              sum+=Math.pow((profits[i]-mean),2);
  
          }
          mean=sum/(n-1);
          double deviation=Math.sqrt(mean);
          return deviation;
      }
  
      public void displayStanderedDeviation() {
      double StanderedDeviation = getStanderedDeviation();
      System.out.println(&quot;the standard deviation is &quot; + StanderedDeviation);
      System.out.println();
      }
  
          public void displayInfoInDescendingOrder () {
              String nameCopy[] = names;
              double profitCopy[] = profits;
              for (int i = 0; i &lt; nameCopy.length; i++) {
  
                  for (int j = i + 1; j &lt; nameCopy.length; j++) {
  
                      if (profitCopy[i] &gt; profitCopy[j]) {
                          double temp = profitCopy[i];
                          profitCopy[i] = profitCopy[j];
                          profitCopy[j] = temp;
                          String nameTemp = nameCopy[i];
                          nameCopy[i] = nameCopy[j];
                          nameCopy[j] = nameTemp;
                      }
                  }
              }
              System.out.printf(&quot;%-20s%s\n&quot;, &quot;City&quot;, &quot;Profit&quot;);
              String profit;
              System.out.println(&quot;Cities and profit details is descending order&quot;);
              for (int i = 0; i &lt; nameCopy.length; i++) {
                  profit = formatter.format(profitCopy[i]);
                  System.out.printf(&quot;%-20s%s\n&quot;, nameCopy[i], profit);
              }
              System.out.println();
          }
      }
  </code></pre>
  <p>this is my main class</p>
  <pre><code>
  public class TestSupermarket {
      public static void main(String[] args) {
          String cities[] = {&quot;Miami&quot;,&quot;Sunrise&quot;,&quot;Hollywood&quot;,&quot;Tallahassee&quot;,
                  &quot;Jacksonville&quot;,&quot;Orlando&quot;,&quot;Gainesville&quot;,&quot;Pensacola&quot;,&quot;Ocala&quot;,&quot;Sebring&quot;
          };
          double profit[] ={10200000,14600000,17000000,6000000,21600000,
                  9100000,8000000,12500000,2000000,4500000};
          classes2.Supermarkets markets =new classes2.Supermarkets(cities, profit);
          markets.displayStatus();
          markets.displayAverage();
          markets.displayHighProfitCity();
          markets.displayCityAboveAverageProfit();
          markets.displayStanderedDeviation();
          markets.displayInfoInDescendingOrder();
      }
  }
  </code></pre>