用文件初始化二维数组

时间:2019-03-14 01:49:25

标签: java

我在弄清楚我应该做的事情时遇到了麻烦。基本上,我应该获取一个包含城市名称列表以及每个城市的总数的文件。我已经创建了一个可以使用预初始化列表的程序,但是现在我的教授要求我们修改它以接收文件... 如果有人可以帮助,我将非常感激!

package assignment7;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

/*******************************************************************************
 * The program first initializes the arrays using a constructor method     and 
 * then calculates the total sales for each city and month and prints     the 
 * formatted table out to the user.
  *******************************************************************************/

public class Assignment7 {

private static String[] months;
private static String[] cities;
private static int[] citySum;
private static int[] monthlySum;
private static int[][] sales;
private static int col;
private static int row;

public static void main(String[] args) {

    Assignment7 sale = new Assignment7();
    sale.calCityTotal();
    sale.calMonthlyTotal();
    sale.displayTable();
}

/***********************************************************
 * Constructor method for Sales.java
 ***********************************************************/

public Assignment7() {

    File file = new File("Sales.txt");

    try {
    PrintWriter output = new PrintWriter(file);
    output.print("7 6\n" +
                 "Chilliwack\n" +
                 "Kamloops\n" +
                 "Kelowna\n" +
                 "Nanaimo\n" +
                 "Surrey\n" +
                 "Vancouver\n" +
                 "Victoria\n" +
                 "400 500 500 600 500 600 \n" +
                 "600 800 800 800 900 900 \n" +
                 "700 700 700 900 900 1000 \n" +
                 "500 600 700 800 700 700 \n" +
                 "800 700 800 700 900 800 \n" +
                 "1000 1100 1200 1300 1400 1400 \n" +
                 "900 900 900 1000 1100 1100 ");
    output.close();
    Scanner scan = new Scanner(file);

    months = new String[6];
    cities = new String[7];
    sales = new int[7][6];
    citySum = new int[sales.length];
    monthlySum = new int[sales.length];

    }
    catch (IOException exception) {

        System.out.println("File error.");
    }
}

/***********************************************************
 * This method calculates the total sales for each city
 * and stores the values in an array.
 ***********************************************************/

public void calCityTotal() {

    for (row = 0; row < sales.length; row++) {
        for (col = 0; col < sales[0].length; col++) {
            citySum[row] += sales[row][col];
        }
    }
}

/***********************************************************
 * This method calculates the total sales for each month and
 * stores the values in an array.
 ***********************************************************/

public void calMonthlyTotal() {

    for (row = 0; row < sales.length; row++) {
        for (col = 0; col < sales[0].length; col++) {
            monthlySum[col] += sales[row][col];
        }
    }
    for (int row = 0; row < citySum.length; row++) {
        monthlySum[6] += citySum[row];
    }
}

/***********************************************************
 * This method creates a formatted version of a table so the
 * user will easily be able to see the data and displays it
 * to the user.
 ***********************************************************/

public void displayTable() {

    System.out.println("Store\t\t\t  Monthly Sales\t\t\t\tStore");
    System.out.println("Location\t\t (thousands of $)\t\t\tTotals");
    System.out.print("\t\t");

    for (int col = 0; col < sales[0].length; col++) {
        System.out.print(months[col]);
        System.out.print("\t");
    }

    for (int row = 0; row < sales.length; row++) {
        System.out.print("\n" + cities[row]);
        for (int col = 0; col < sales[0].length; col++) {
            System.out.print("\t" + sales[row][col]);
        }
        System.out.print("\t" + citySum[row]);
    }

    System.out.print("\nMonth totals\t");

    for (int row = 0; row < sales.length; row++) {
        System.out.print(monthlySum[row] + "\t");
    }
}

}

0 个答案:

没有答案