###前提・実現したいこと
csvファイルから読み込んだものをソートして別のcsvファイルに書き出しを行いたいです。
###発生している問題・エラーメッセージ
書き出しが行われない。
###該当のソースコード
java
1import java.io.*; 2import java.io.FileReader; 3import java.io.BufferedReader; 4import java.io.IOException; 5import java.util.ArrayList; 6import java.util.*; 7 8public class Kadai71{ 9 public static void main (String[] args) throws FileNotFoundException, IOException{ 10 StudentList stl = new StudentList(); 11 stl.readData("kadai71i.csv"); 12 stl.sortData(); 13 System.out.println("ソート後--------------------------"); 14 System.out.println("学籍番号 氏名 国語 数学 英語 合計"); 15 stl.show(); 16 stl.writeData("kadai71o.csv"); 17 } 18} 19 20/** 21*学籍番号、氏名、国語、英語、数学の点数、合計点を格納するStudentクラス 22*/ 23class Student { 24 25 /** 26 *学籍番号を表します 27 */ 28 protected int number; 29 30 /** 31 *氏名を表します 32 */ 33 protected String name; 34 35 /** 36 *国語の点数を表します 37 */ 38 protected int japanese; 39 40 /** 41 *数学の点数を表します 42 */ 43 protected int math; 44 45 /** 46 *英語の点数を表します 47 */ 48 protected int english; 49 50 /** 51 *合計点を表します 52 */ 53 protected int total; 54 55 /** 56 *学籍番号、氏名、国語、数学、英語の点数を設定するコンストラクタ 57 */ 58 public Student(int n, String N, int j, int m, int e){ 59 number = n; 60 name = N; 61 japanese = j; 62 math = m; 63 english = e; 64 total = j+m+e; 65 } 66 67 68 /** 69 *学籍番号を返すメソッド 70 */ 71 public int getNumber(){ 72 return number; 73 } 74 75 /** 76 *氏名を返すメソッド 77 */ 78 public String getName(){ 79 return name; 80 } 81 82 /** 83 *国語の点数を返すメソッド 84 */ 85 public int getJapanese(){ 86 return japanese; 87 } 88 89 /** 90 *数学の点数を返すメソッド 91 */ 92 public int getMath(){ 93 return math; 94 } 95 96 /** 97 *英語の点数を返すメソッド 98 */ 99 public int getEnglish(){ 100 return english; 101 } 102 103 /** 104 *合計点を返すメソッド 105 */ 106 public int getTotal(){ 107 return total; 108 } 109 110 /** 111 *学生の情報を表示するメソッド 112 */ 113 public void show(){ 114 System.out.println(number+ "," +name+ "," +japanese+ "," +math+ "," +english+ ","+total); 115 } 116} 117 118/** 119*Studentクラスを扱うクラスStudentList 120*/ 121class StudentList{ 122 /** 123 *ArrayList<String>型のprotectedメンバーの実装 124 */ 125 protected ArrayList<Student> al = new ArrayList<Student>(); 126 127 128 /** 129 *ファイル名を引数として、ファイルの内容をArrayListに保存する 130 */ 131 public void readData(String name){ 132 String line; 133 String[] s; 134 try{ 135 FileReader fr = new FileReader(name); 136 BufferedReader in = new BufferedReader(fr); 137 int i = 0; 138 System.out.println("入力ファイル----------------------------"); 139 System.out.println("学籍番号 氏名 国語 数学 英語 合計"); 140 while((line = in.readLine()) != null){ 141 s = line.split(","); 142 al.add(new Student( 143 Integer.parseInt(s[0]), 144 s[1], 145 Integer.parseInt(s[2]), 146 Integer.parseInt(s[3]), 147 Integer.parseInt(s[4])) 148 ); 149 i=i+1; 150 int t = Integer.parseInt(s[2])+ Integer.parseInt(s[3]) +Integer.parseInt(s[4]); 151 System.out.println(s[0]+" "+s[1]+" "+s[2]+" "+s[3]+" "+s[4]+" "+t); 152 } 153 fr.close(); 154 }catch(IOException e){ 155 } 156 } 157 158 /** 159 *ArrayListに格納されたデータを合計点の高い順にソートするメソッド 160 */ 161 public void sortData(){ 162 int n = al.size(); 163 while(n>0){ 164 int i=0; 165 for(int j = 0; j<n-1; j++){ 166 Student st1 = (Student)al.get(j); 167 Student st2 = (Student)al.get(j+1); 168 if(st1.getTotal() < st2.getTotal()){ 169 al.set(j,st2); 170 al.set(j+1,st1); 171 i = j+1; 172 } 173 } 174 n = i; 175 } 176 } 177 178 public void show(){ 179 for(int i = 0; i<al.size(); i++){ 180 Student st = (Student)al.get(i); 181 System.out.println(st.getNumber()+" "+st.getName()+" "+st.getJapanese()+" " 182 +st.getMath()+" "+st.getEnglish()+" "+st.getTotal()); 183 } 184 } 185 186 public void writeData(String name) { 187 try { 188 BufferedWriter in = new BufferedWriter(new FileWriter(name)); 189 for (int i=0;i<al.size();i++) { 190 in.write(al.get(i).getNumber() + "," + al.get(i).getName() + "," 191 + al.get(i).getJapanese() + "," + al.get(i).getMath() + "," 192 + al.get(i).getEnglish() + "," + al.get(i).getTotal()); 193 in.newLine(); 194 } 195 196 197 } catch (Exception e) { 198 } 199 } 200 201} 202 203 204
###試したこと
###補足情報(言語/FW/ツール等のバージョンなど)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/23 05:35