2016-04-26 12 views
0

私の問題の修正を検索しましたが、動作していないようです。 1つの特定の参照番号:thisおよびthis、特にthis。しかし、私はそれらをどのように実装しても、私は理解できないOutOfBoundsErrorを受け取ります。2次元配列の列と行の合計

このプログラムは、クラスの特別な単位です。実際には、それは非常に簡単です -

プログラムの説明:次の問題を解決するには、2次元配列を使用します。会社には、5種類の製品(1〜5)を販売する4人の営業担当者(1〜4人)がいます。 1日に1回、各販売員は、販売された種類の製品ごとに伝票を渡します。各スリップは含まれています

販売者番号
製品番号
その製品の総ドルの値がこのようにその日に


を販売し、各営業担当者は一日あたり0と5の販売伝票の間に渡されます。先月のすべての伝票の情報が利用可能であると仮定します。各データ行には、3つの数字(営業担当者番号、製品番号、売上)が含まれています。

先月の売上に関するこのすべての情報を読み、製品ごとの売上高の合計を要約するプログラムを作成します。

提供されたデータ:

1 2 121.77 
1 4 253.66 
1 5 184.22 
1 1 97.55 
2 1 152.44 
2 2 104.53 
2 4 189.97 
2 5 247.88 
3 5 235.87 
3 4 301.33 
3 3 122.15 
3 2 301.00 
3 1 97.55 
4 1 125.66 
4 2 315.88 
4 4 200.10 
4 3 231.45 

それが列を計算しようとするとエラーにのみ付属しています。私の行は動作します。どのように私はforループまたは配列の行または列のindecesのいずれかを変更しても、それは動作しません。私は最初に私の行を別々に計算してから、私の列の合計を計算しました。私が見落としていることがはっきりと見落とされています。ここで

は私のコードです:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.text.DecimalFormat; 
import java.util.Scanner; 


public class prog480u { 

    static Scanner inFile = null; 

    public static void main(String[] args) { 

     try { 

      // create scanner to read file 
      inFile = new Scanner(new File ("prog480u.dat")); 

     } catch (FileNotFoundException e) { 
      System.out.println("File not found!"); 
      System.exit(0); 
     } 

     // make the array 

     int x = 0; 
     int y = 0; 

     double[][] profits = new double[4][5]; 

     while (inFile.hasNext()) { 

      x = inFile.nextInt(); // use sales numbers as coordinates 
      y = inFile.nextInt(); 

       profits[x - 1][y - 1] = inFile.nextDouble();    

      } 


     // check if it's okay 


     System.out.println(""); 
     double[][] columnProfits = sums(profits); 

     for (int a = 0; a < columnProfits.length; a++) { 
      System.out.print((a+1) + "\t"); 
      for (int b = 0; b < columnProfits[a].length; b++) { 
      System.out.print(columnProfits[a][b] + "\t"); 
     } 

     System.out.println(""); 
     } 

     double[] bottomRow = columnSums(columnProfits); 

     for (int a = 0; a < bottomRow.length; a++) { 

      System.out.print("Total:" + bottomRow + "\t"); 

     } 


    } 

    public static double[][] sums (double[][] q) { 

     double[][] array = new double[5][6]; 
     array = q; 

     double sum = 0;  

     for (int a = 0; a < array.length; a++) { 

      for (int b = 0; b < array[0].length; b ++) { 

       sum += array[a][b]; // add everything in the row 


      } 

      array[a][4] = sum; // set that row to the last column 

      sum = 0; // reset sum to 0 

     } 

     return array; 
    } 

    public static double[] columnSums (double[][]q) { 

     double[][] array = new double[5][6]; 
     array = q; 

     double sum2 = 0; 

     double[] columns = new double [5]; 

     for (int a = 0; a < array.length; a++) { 

      for (int b = 0; b < array[0].length; b ++) { 

       sum2 += array[b][a]; 

       columns[b] = sum2; 

      } 

      sum2 = 0; // reset sum to 0 

     } 

     return columns; 
    } 


} 

はお時間をありがとうございました。私のプログラムは仕事に近づいていると感じていますが、この小さな間違いが私を突っ込んでいます。

+0

あなたは「+ =」を必要とするように見え、ない「=」あなたは、二重 – mcraen

答えて

2

はここで作業コード(私は少しそれをクリーンアップ)です:あなたは非常に接近していた

、あなただけのループのために、あなたの最大なインデックスを交換する必要がありました。あなただけの楽しみのためにjava.lang.ArrayIndexOutOfBoundsException

public static double[] columnSums(double[][] q) 
{ 
    double[][] array = q; 

    double[] columns = new double[5]; 

    for (int a = 0; a < array[0].length; a++) 
    { 
     for (int b = 0; b < array.length; b++) 
     { 
      columns[a] += array[b][a]; 
     } 
    } 
    return columns; 
} 
+0

良さを読んだときに、そんなにありがとう!私はそれが私をひどく捨てたような小さな誤りだとは信じられません。私はあなたに多くの良い日をお祈りします。 – Nikolas

1

を取得した理由です、私はそのためのオブジェクト指向のバージョンを書きました。 扱いやすいシステムが追加機能を必要としたら:

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map.Entry; 



class Sale { 

    static public ArrayList<Sale> readSales(final String pSalesFileName) throws FileNotFoundException, IOException { 
     final ArrayList<Sale> ret = new ArrayList<>(); 
     try (final BufferedReader br = new BufferedReader(new FileReader(pSalesFileName))) { 
      int lineIndex = 0; 
      while (true) { 
       ++lineIndex; 
       final String line = br.readLine(); 
       if (line == null) { 
        System.out.println("Line #" + lineIndex + " is empty, skipping..."); 
        break; 
       } 

       try { 
        final String[] values = line.split("\\s"); 
        final int salesPersonId = Integer.parseInt(values[0]); 
        final int productId = Integer.parseInt(values[1]); 
        final float sales = Float.parseFloat(values[2]); 
        final Sale sale = new Sale(salesPersonId, productId, sales); 
        ret.add(sale); 

       } catch (final ArrayIndexOutOfBoundsException e) { 
        System.err.println("Parse error in line #" + lineIndex + ": '" + line + "'"); 
       } 
      } 
     } 
     return ret; 
    } 



    private final int mSalesPersonId; 
    private final int mProductId; 
    private final float mSales; 

    public Sale(final int pSalesPersonId, final int pProductId, final float pSales) { 
     mSalesPersonId = pSalesPersonId; 
     mProductId = pProductId; 
     mSales = pSales; 
    } 

    public Integer getSalesPersonId_R() { 
     return Integer.valueOf(mSalesPersonId); 
    } 
    public Integer getProductId_R() { 
     return Integer.valueOf(mProductId); 
    } 
    public float getSales() { 
     return mSales; 
    } 
} 



class SalesPerson { 
    private final HashMap<Integer, ArrayList<Sale>> mSalesMap = new HashMap<>(); 

    private final int        mId; 

    public SalesPerson(final int pId) { 
     mId = pId; 
    } 

    @Override public boolean equals(final Object pObj) { 
     if (!(pObj instanceof SalesPerson)) return false; 
     return ((SalesPerson) pObj).mId == mId; 
    } 
    @Override public int hashCode() { 
     return mId; 
    } 

    public void addSale(final Sale pSale) { 
     final Integer productId = pSale.getProductId_R(); 
     ArrayList<Sale> salesList = mSalesMap.get(productId); 
     if (salesList == null) { 
      salesList = new ArrayList<>(); 
      mSalesMap.put(productId, salesList); 
     } 
     salesList.add(pSale); 
    } 
    public Integer getId_R() { 
     return Integer.valueOf(mId); 
    } 
    public HashMap<Integer, ArrayList<Sale>> getSalesMap() { 
     return mSalesMap; 
    } 

    public float getSalesTotalByProductId(final Integer pProductId) { 
     final ArrayList<Sale> sales = mSalesMap.get(pProductId); 
     float accumulator = 0; 
     for (final Sale sale : sales) { 
      accumulator += sale.getSales(); 
     } 
     return accumulator; 
    } 
} 



public class SalesFun { 

    public static void main(final String[] args) throws FileNotFoundException, IOException { 
     final ArrayList<Sale> sales = Sale.readSales("test/sales.txt"); 

     final HashMap<Integer, SalesPerson> personMap = new HashMap<>(); 
     for (final Sale sale : sales) { 
      // find right salesperson or create new, then add sale to it 
      final Integer salesPersonId = sale.getSalesPersonId_R(); 
      SalesPerson person = personMap.get(salesPersonId); 
      if (person == null) { 
       person = new SalesPerson(salesPersonId.intValue()); 
       personMap.put(salesPersonId, person); 
      } 
      person.addSale(sale); 
     } 

     printSales(personMap); 
    } 

    static private void printSales(final HashMap<Integer, SalesPerson> pPersonMap) { 
     for (final SalesPerson person : pPersonMap.values()) { 
      System.out.println("SalesMan ID: " + person.getId_R()); 
      for (final Entry<Integer, ArrayList<Sale>> entry : person.getSalesMap().entrySet()) { 
       final Integer productId = entry.getKey(); 
       final float sales = person.getSalesTotalByProductId(productId); 
       System.out.println("\tProduct ID: " + entry.getKey() + "\tSales: " + sales); 
      } 
     } 
    } 

} 
+0

こんにちは!ありがとう、これはとても奇妙で興味深いです。私はそれを保持し、遠く離れた日にそれを理解することを願っています。 – Nikolas