0

Hi, I'm working on a project where I have two classes, Divisions and SalesStats. I need to read in a text file from a method in SalesStats and store that data into two dimensional arrays to Divisions. When I execute, only 0.0 is coming out and not the numbers from
the text file. Can someone help me/guide me to what I'm doing wrong?

DIVISION 1
Quarter 1 :
0.0
Quarter 2 :
0.0
Quarter 3 :
0.0
Quarter 4 :
0.0

public class Divisions
{
   public final int DIVISION = 6;
   public final int QUARTERS = 4;
   private static double[][] sales;
   public static void setSales(int div, int qrt, double amount)
   {
      double[][] sales = new double[6][4]; // This might be the problem
      amount = sales[div][qrt];
   }
   public static double getSales(int div, int qrt)
   {
      double[][] sales = new double[6][4]; // This might be the problem
      return sales[div][qrt];
   }
}
public class SalesStats
{
   public static void main(String args[]) throws IOException
   {
      Divisions d = new Divisions();
      readValues(d);
      displaySales(d);
...
   public static void readValues(Divisions d) throws IOException
   {
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Please enter input filename: ");
      String input = keyboard.nextLine();

      File myfile = new File(input);
      if (!myfile.exists())
      {
         System.out.println("input file " + input + " does not exist");
         System.exit(0);
      }
      Scanner inputFile = new Scanner(myfile);
      for (int i = 0; i < d.DIVISION; i++)
         for (int j = 0; j < d.QUARTERS; j++)
         {
            Divisions.setSales(i,j,inputFile.nextDouble());
         }
      inputFile.close();
   }
   public static void displaySales(Divisions d)
   {
...
      for (int i = 0; i < d.DIVISION; i++)
      {
         System.out.println("DIVISION " + (i+1));
         for (int j = 0; j < d.QUARTERS; j++)
         {
            System.out.println("Quarter " + (j+1) + " : ");
            System.out.println(Divisions.getSales(i,j));
         }
      }
   }

Thank you!

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by ceyesuma
0

This example may help.
You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of square brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values.

In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length, as shown in the following MultiDimArrayDemo program:

class MultiDimArrayDemo {
        public static void main(String[] args) {
            String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                                {"Smith", "Jones"}};
            System.out.println(names[0][0] + names[1][0]); //Mr. Smith
            System.out.println(names[0][2] + names[1][1]); //Ms. Jones
        }
    }

The output from this program is:

Mr. Smith
Ms. Jones

Finally, you can use the built-in length property to determine the size of any array. The code

System.out.println(anArray.length);

will print the array's size to standard output.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.