JAVA初心者です。
以前作成したプログラムの改修をしています。
内容は、2つのCSVデータを使ってファイル入出力のプログラムです。
商品マスタのデータと購入明細ファイルデータの商品コードを
比較し一致したら、出力ファイルに購入明細と商品マスタにある
必要なデータを抽出し出力するというものですが、
今回はファイルを読み込みながら配列を使って商品コードを比較
することを試みています。
【解決したいこと】
1、商品マスタのデータは2次元配列に格納し、購入明細の
商品コードと比較するにはどうすればよろしいですか?
2、配列に格納されている引数をint num2の変数に格納したいのですが、
「型 Integer のメソッド parseInt(String) は引数 (String[]) に
適用できません。」とEclipseに怒られます。
何か良い方法はありますでしょうか?
package lessons; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; public class StaffMonthlySalesRecord5 { public static void main(String[] args) { BufferedReader brInputFileA= null ; //商品マスタ用入力ファイル BufferedReader brInputFileM= null ; //購入明細用入力ファイル BufferedWriter bwOutputFileA = null ; //月額購入表出力ファイル String stBufferM = ""; //inMファイルデータを格納 String stBufferA = ""; //inAファイルデータを格納 String[] itemA = null; ArrayList<String[]> al = new ArrayList<String[]>(); String[][] itemM ; String date = ""; //売上明細ファイルの購入月をセット String staff = ""; //売上明細ファイルの担当者をセット boolean isFirst = true; //0番目の売上日をセットする為の変数 boolean isFirstStaff = true; //0番目の担当者をセットする為の変数 int sum = 0; //数量×単価=合計金額 int allSum = 0; //月の合計金額 int cnt15 = 0; //15件を数えるカウンター final String noItem = "商品マスタ未存在"; final String noCalendar = "日付に誤りがあります"; try{ fileError:{ try{ brInputFileA = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/temp/Desktop/inA.csv"),"JISAutoDetect")); }catch(FileNotFoundException e){ System.err.println("inA.csvファイルが見つかりません。"); break fileError; } try{ bwOutputFileA = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/Users/temp/Desktop/outA.csv"),"SJIS")); }catch(FileNotFoundException e){ System.err.println("outA.csvファイルが見つかりません。"); break fileError; } bwOutputFileA.write("担当者" + "," + "売上日" + "," + "商品名" + "," + "数量" + " ," + "売上金額" + "," + "備考"); //項目名出力 bwOutputFileA.newLine(); boolean emptyflag = true; //ファイル空判定のフラグ //inAファイルデータを読み込み while((stBufferA = brInputFileA.readLine()) != null){ emptyflag = false; if(stBufferA.equals("")){ System.err.println("データに不備があります。"); break fileError; } if(stBufferA.isEmpty()){ System.err.println("inA.csvファイルは空です。"); break fileError; } itemA = stBufferA.split(","); if(stBufferA.equals("")){ System.err.println("データに不備があります。"); break fileError; } if(isFirst){ date = itemA[1].substring(0, 6); isFirst = false; } String box = ""; //担当者出力用変数 if(isFirstStaff){ staff = itemA[0]; isFirstStaff = false; staff = itemA[0]; //担当者再セット box = staff; //担当者出力用 }else{ if(itemA[0].equals(staff)){ box = ""; }else{ bwOutputFileA.write("*"); bwOutputFileA.newLine(); bwOutputFileA.write(date.substring(0, 6) + "月合計" + "," + "," + "," + "," + allSum); //合計金額を出力 bwOutputFileA.newLine(); String a = " "; bwOutputFileA.write(a); bwOutputFileA.newLine(); date = itemA[1]; //売上日再セット allSum = 0; // 月の合計を0に戻す staff = itemA[0]; //担当者再セット box = staff; //担当者出力用 cnt15 = 0; // 件数カウンターを0に戻す } } String soldDay = (itemA[1].substring(0, 6)); int s= soldDay.compareTo(date); // 月変更の比較 if(s > 0){ bwOutputFileA.write("*"); bwOutputFileA.newLine(); bwOutputFileA.write(date.substring(0, 6) + "月合計" + "," + "," + "," + "," + allSum); //合計金額を出力 bwOutputFileA.newLine(); String a = " "; bwOutputFileA.write(a); bwOutputFileA.newLine(); date = itemA[1]; //売上日を再セット allSum = 0; // 月の合計を0に戻す cnt15 = 0; //件数カウンターを0に戻す } try{ brInputFileM = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/temp/Desktop/inM.csv"),"JISAutoDetect")); }catch(FileNotFoundException e){ System.err.println("inMファイルが見つかりません。"); break fileError; } boolean emptyflag2 = true; //ファイル空判定のフラグ //inMファイルデータを読み込み while((stBufferM = brInputFileM.readLine()) != null){ emptyflag2 = false; if(stBufferM.equals("")){ System.err.println("データに不備があります。"); break fileError; } al.add(stBufferM.split(",")); /* ArrayListへ読み込み */ } itemM = new String[al.size()][]; for (int i = 0; i < al.size(); i++) { itemM[i] = al.get(i); /* ArrayListから配列へ格納 */ } if(emptyflag2){ System.err.println("inM.csvファイルは空です。"); break fileError; } int Xq = 0; for (String[] code : itemM) { //商品コードが一致しているか確認 if(code.equals(itemA[2])){ Xq = 1; String b = itemA[1]; //売上日 String[] c = (String[]) itemM[1]; //商品名 String d = itemA[3]; //数量 String e = ""; //備考 String[] f = (String[]) itemM[2]; //単価 if(checkDate(itemA[1])){ //日付の妥当性を確認 int num1 = Integer.parseInt(d); //数量 int num2 = Integer.parseInt(f); //単価 sum = num1 * num2; //数量 × 単価 = 金額 allSum = sum + allSum; //月合計を算出 bwOutputFileA.write(box + "," + b + "," + c + "," + d + "," + sum + "," + e); bwOutputFileA.newLine(); }else{ e = noCalendar; //備考=日付に誤りがあります bwOutputFileA.write(box + "," + b + "," + c + "," + d + "," + sum + "," + e); bwOutputFileA.newLine(); } } } //for if (Xq == 0){ String b = itemA[1]; //売上日 String c = noItem; //商品名=商品マスタ未存在 String d = itemA[3]; //数量 bwOutputFileA.write(box + "," + b + "," + c + "," + d); bwOutputFileA.newLine(); } cnt15 = cnt15 + 1; //件数カウンターに1を加算 if(cnt15 >= 15){ //件数カウンターが15件に到達した場合 String a = " "; bwOutputFileA.write(a); bwOutputFileA.newLine(); cnt15 = 0; // 件数カウンターリセット } }//while if(emptyflag){ System.err.println("inA.csvファイルは空です。"); break fileError; } bwOutputFileA.write("*"); bwOutputFileA.newLine(); bwOutputFileA.write(date.substring(0, 6) + "月合計" + "," + "," + "," + "," + allSum); //合計金額を出力 }//fileError } //try catch(IOException e){ System.err.println("エラーが発生しました。"); e.printStackTrace(); } finally{ try { if(brInputFileA != null){ brInputFileA.close(); } if(brInputFileM != null){ brInputFileM.close(); } if(bwOutputFileA != null){ bwOutputFileA.close(); } } catch (IOException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } }//finally }//main /** * 日付の妥当性をチェック * カレンダーに存在するかどうかを返す。 * 存在する日付の場合true */ public static boolean checkDate(String strDate) { if (strDate == null || strDate.length() != 8) { throw new IllegalArgumentException( "引数の文字列["+ strDate +"]" + "は不正です。"); } DateFormat format = new SimpleDateFormat("yyyyMMdd"); format.setLenient(false); try { format.parse(strDate); return true; } catch (Exception e) { return false; } } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。