10 Replies Latest reply: Aug 15, 2007 10:44 AM by 807605 RSS

    Array contains no elements... how come??

    807605
      ive taken all your advices and guess what, the code now works!!! thanks to all of you who helped, more power and god bless!!!

      my problem now is on how to output the average... this is a bit tricky for me because i think i have to output it as a double or with decimal numbers...

      the output of average outputs the array contains no elements???

      any ideas???
      ===========================================================
      import java.io.*;
      import javax.swing.*;

      class methodmin{
      public static int makemin (int[] minimum){

      int min;

      // initialize the current minimum
      min = minimum[0];

      // scan the array
      for ( int index=0; index < minimum.length; index++ )
      {
      if ( minimum[ index ] < min )

      min = minimum[ index ] ;

      }
      JOptionPane.showMessageDialog(null,"Minimum of the array is: " + min
      + "\n"
      ,"Single Dimension Array"
      ,JOptionPane.INFORMATION_MESSAGE);

      return min;
      }
      }

      class methodmax{
      public static int makemax (int[] maximum){

      int max;

      // initialize the current maximum
      max = maximum[0];

      // scan the array
      for ( int index=0; index < maximum.length; index++ )
      {
      if ( maximum[ index ] > max ) // examine the current element
      max = maximum[ index ]; // if it is the largest so far, change max

      }
      JOptionPane.showMessageDialog(null,"Maximum of the array is: " + max
      + "\n"
      ,"Single Dimension Array"
      ,JOptionPane.INFORMATION_MESSAGE);
      return max;
      }
      }

      class methodsum{
      public static int makesum (int[] sum){


      // declare and initialize the total
      int total = 0 ;

      // add each element of the array to the total
      for ( int index=0; index < sum.length; index++ )
      {
      total = total + sum[ index ] ;

      }
      JOptionPane.showMessageDialog(null,"Sum of the array is: " + total
      + "\n"
      ,"Single Dimension Array"
      ,JOptionPane.INFORMATION_MESSAGE);
      return total;
      }
      }
      class methodave{
       public static int makeave (int[] ave){
       
          // declare and initialize the total
          int  total = 0 ;
          int  index= 0;
          // add each element of the array to the total
          for (  index=0; index < ave.length ; index++ )
          {
            total =  total + ave[ index ] ;
       
          }
       
          if ( ave.length <= 0 )
          {
             JOptionPane.showMessageDialog(null,"Average of the array is: " + total/ave[index]
                                                                            + "\n"
                                               ,"Single Dimension Array"
                                               ,JOptionPane.INFORMATION_MESSAGE );
          }    
          else
             JOptionPane.showMessageDialog (null,"The array contains no elements "
                                                  ,"Single Dimension Array"
                                               , JOptionPane.INFORMATION_MESSAGE);
       return ave[index];
       }
      }
      class mainmenu{
      public static void main(String[] args) {
      int i=0;
      int rep=0;
      int numba;
      String temp="";

      int[] anArray;
      anArray = new int[10];


      methodmin fmin = new methodmin();
      methodmax fmax = new methodmax();
      methodsum fsum = new methodsum();
      methodave fave = new methodave();

      JOptionPane.showMessageDialog(null,"This is a program that will \n"
      + "display the Minimum, Maximum, \n"
      + "Sum and Average numbers in a \n"
      + "Single Dimension Array \n"
      + "Using Objects and Classes"
      ,"Single Dimension Array"
      ,JOptionPane.INFORMATION_MESSAGE);
      do{

      for(i=0; i<=9; i++)
      {
      anArray=Integer.parseInt(JOptionPane.showInputDialog (null,"Enter ten numbers"
      ,"Single Dimension Array"
      , JOptionPane.QUESTION_MESSAGE));
      }

      numba = anArray[0];
      for(i=0; i<=9; i++)
      {

      numba = anArray;
      temp=temp+"\n"+numba;
      }

      JOptionPane.showMessageDialog(null,"The Numbers Entered Are: " + temp
      + "\n"
      ,"Single Dimension Array"
      ,JOptionPane.INFORMATION_MESSAGE);

      fmin.makemin(anArray);
      fmax.makemax (anArray);
      fsum.makesum(anArray);
      fave.makeave(anArray);

      rep = JOptionPane.showConfirmDialog (null,"Input another number?"
      ,"Single Dimension Array"
      ,JOptionPane.YES_NO_OPTION);
      }
      while(rep==0);

      JOptionPane.showMessageDialog(null,"Reading Arrays"+"\n Programmed by: Carl Anthony P. Estepa"
      ,"Single Dimension Array"
      , JOptionPane.INFORMATION_MESSAGE);
      }
      }