前提
Javaの勉強をしています。
以前、「4行で表されている選手成績を1行でコンソールに表示したい」について回答を頂き一度は解決しました。
しかし、より改善したいと思い、方法を考えているのですが方法がわかりません。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- 1行目から4行目にある項目を1個の配列に格納したい。
- そのうち4行目にある24項目もそれぞれ分割し、要素数27の配列として、1つの配列に入れたい。
- 4行目の24項目にひとつでも数値がはいってなければ、エラーを表示するなどするようにして、行が1つ抜けているなどの場合に気づけるよう精度を高めたい。
該当のソースコード
メインクラス
java
1package baseball; 2 3import java.io.BufferedReader; 4import java.io.File; 5import java.io.FileInputStream; 6import java.io.InputStreamReader; 7import java.util.ArrayList; 8import java.util.List; 9 10public class Challenge5 { 11 private static final String FILE_PATH = "C:\\Users\\user name \\Documents\\workspace-spring-tool-suite-4-4.15.3.RELEASE\\test 1st\\dat\\pro.csv"; 12 13 public static void main(String[] args) { 14 File file = new File(FILE_PATH); 15 16 try (FileInputStream fis = new FileInputStream(file); 17 InputStreamReader isr = new InputStreamReader(fis); 18 BufferedReader br = new BufferedReader(isr);) { 19 20 21 List<Pro> pros = new ArrayList<>(); 22 String line; 23 24 25 while ((line = br.readLine()) != null) { 26 String[] strs = {line,br.readLine(), br.readLine(), br.readLine() }; 27 28 // コンストラクタをオーバロードして呼び出し 29 Pro pro = new Pro(strs); 30 pros.add(pro); 31 32 } 33 34 for (Pro pro1 : pros) { 35 System.out.println(pro1); 36 37 } 38 39 } catch (Exception e) { 40 e.printStackTrace(); 41 42 } 43 } 44} 45 46
Proクラス
java
1package baseball; 2 3public class Pro { 4 private String 順位; 5 private String 選手名; 6 private String 球団; 7 private String 打率; 8 //4行目2列目以降の各データ 9 private String 試合; 10 private String 打席; 11 private String 打数; 12 private String 安打; 13 private String 二塁打; 14 private String 三塁打; 15 private String 本塁打; 16 private String 塁打; 17 private String 打点; 18 private String 得点; 19 private String 三振; 20 private String 四球; 21 private String 死球; 22 private String 犠打; 23 private String 犠飛; 24 private String 盗塁; 25 private String 盗塁死; 26 private String 併殺打; 27 private String 出塁率; 28 private String 長打率; 29 private String OPS; 30 private String 得点圏; 31 private String 失策; 32 33 //読みこんだデータの格納 34 public Pro(String[] pros) { 35 this.set順位(pros[0]); 36 this.set選手名(pros[1]); 37 this.set球団(pros[2]); 38 this.set打率(pros[3]); 39 //4行目2列目以降のデータ 40 this.set試合(pros[4]); 41 this.set打席(pros[5]); 42 this.set打数(pros[6]); 43 this.set安打(pros[7]); 44 this.set二塁打(pros[8]); 45 this.set三塁打(pros[9]); 46 this.set本塁打(pros[10]); 47 this.set塁打(pros[11]); 48 this.set打点(pros[12]); 49 this.set得点(pros[13]); 50 this.set三振(pros[14]); 51 this.set四球(pros[15]); 52 this.set死球(pros[16]); 53 this.set犠打(pros[17]); 54 this.set犠飛(pros[18]); 55 this.set盗塁(pros[19]); 56 this.set盗塁死(pros[20]); 57 this.set併殺打(pros[21]); 58 this.set出塁率(pros[22]); 59 this.set長打率(pros[23]); 60 this.setOPS(pros[24]); 61 this.set得点圏(pros[25]); 62 this.set失策(pros[26]); 63 } 64 65 public String get順位() { 66 return 順位; 67 } 68 69 public void set順位(String 順位) { 70 this.順位 = 順位; 71 } 72 73 public String get選手名() { 74 return 選手名; 75 } 76 77 public void set選手名(String 選手名) { 78 this.選手名 = 選手名; 79 } 80 81 public String get球団() { 82 return 球団; 83 } 84 85 public void set球団(String 球団) { 86 this.球団 = 球団; 87 } 88 89 public String get打率() { 90 return 打率; 91 } 92 93 public void set打率(String 打率) { 94 this.打率 = 打率; 95 } 96 97 public String get試合() { 98 return 試合; 99 } 100 101 public void set試合(String 試合) { 102 this.試合 = 試合; 103 } 104 105 public String get打席() { 106 return 打席; 107 } 108 109 public void set打席(String 打席) { 110 this.打席 = 打席; 111 } 112 113 public String get打数() { 114 return 打数; 115 } 116 117 public void set打数(String 打数) { 118 this.打数 = 打数; 119 } 120 121 public String get安打() { 122 return 安打; 123 } 124 125 public void set安打(String 安打) { 126 this.安打 = 安打; 127 } 128 129 public String get二塁打() { 130 return 二塁打; 131 } 132 133 public void set二塁打(String 二塁打) { 134 this.二塁打 = 二塁打; 135 } 136 137 public String get三塁打(){ 138 return 三塁打; 139 } 140 141 public void set三塁打(String 三塁打) { 142 this.三塁打 = 三塁打; 143 } 144 145 public String get本塁打(){ 146 return 本塁打; 147 } 148 149 public void set本塁打(String 本塁打) { 150 this.本塁打 = 本塁打; 151 } 152 153 public String get塁打(){ 154 return 塁打; 155 } 156 157 public void set塁打(String 塁打) { 158 this.塁打 = 塁打; 159 } 160 161 public String get打点(){ 162 return 打点; 163 } 164 165 public void set打点(String 打点) { 166 this.打点 = 打点; 167 } 168 169 public String get得点(){ 170 return 得点; 171 } 172 173 public void set得点(String 得点) { 174 this.得点 = 得点; 175 } 176 177 public String get三振(){ 178 return 三振; 179 } 180 181 public void set三振(String 三振) { 182 this.三振 = 三振; 183 } 184 185 public String get四球(){ 186 return 四球; 187 } 188 189 public void set四球(String 四球) { 190 this.四球 = 四球; 191 } 192 193 public String get死球(){ 194 return 死球; 195 } 196 197 public void set死球(String 死球) { 198 this.死球 = 死球; 199 } 200 201 public String get犠打(){ 202 return 犠打; 203 } 204 205 public void set犠打(String 犠打) { 206 this.犠打 = 犠打; 207 } 208 209 public String get犠飛(){ 210 return 犠飛; 211 } 212 213 public void set犠飛(String 犠飛) { 214 this.犠飛 = 犠飛; 215 } 216 217 public String get盗塁(){ 218 return 盗塁; 219 } 220 221 public void set盗塁(String 盗塁) { 222 this.盗塁 = 盗塁; 223 } 224 225 public String get盗塁死(){ 226 return 盗塁死; 227 } 228 229 public void set盗塁死(String 盗塁死) { 230 this.盗塁死 = 盗塁死; 231 } 232 233 public String get併殺打(){ 234 return 併殺打; 235 } 236 237 public void set併殺打(String 併殺打) { 238 this.併殺打 = 併殺打; 239 } 240 241 public String get出塁率(){ 242 return 出塁率; 243 } 244 245 public void set出塁率(String 出塁率) { 246 this.出塁率 = 出塁率; 247 } 248 249 public String get長打率(){ 250 return 長打率; 251 } 252 253 public void set長打率(String 長打率) { 254 this.長打率 = 長打率; 255 } 256 257 public String getOPS(){ 258 return OPS; 259 } 260 261 public void setOPS(String OPS) { 262 this.OPS = OPS; 263 } 264 265 public String get得点圏(){ 266 return 得点圏; 267 } 268 269 public void set得点圏(String 得点圏) { 270 this.得点圏 = 得点圏; 271 } 272 273 public String get失策(){ 274 return 失策; 275 } 276 277 public void set失策(String 失策) { 278 this.失策 = 失策; 279 } 280 281 //オーバーライド 282 public String toString() { 283 return 284 "順位=" + this.get順位() + 285 ", 選手名=" + this.get選手名() + 286 ", 球団=" + this.get球団() + 287 ", 打率=" + this.get打率() + 288 ", 試合=" + this.get試合() + 289 ", 打席=" + this.get打席() + 290 ", 打数=" + this.get打数() + 291 ", 安打=" + this.get安打() + 292 ", 二塁打=" + this.get二塁打() + 293 ", 三塁打=" + this.get三塁打() + 294 ", 本塁打=" + this.get本塁打() + 295 ", 塁打=" + this.get塁打() + 296 ", 打点=" + this.get打点() + 297 ", 得点=" + this.get得点() + 298 ", 三振=" + this.get三振() + 299 ", 四球=" + this.get四球() + 300 ", 犠打=" + this.get犠打() + 301 ", 犠飛=" + this.get犠飛() + 302 ", 盗塁=" + this.get盗塁() + 303 ", 併殺打=" + this.get併殺打() + 304 ", 出塁率=" + this.get出塁率() + 305 ", 長打率=" + this.get長打率() + 306 ", OPS=" + this.getOPS() + 307 ", 得点圏=" + this.get得点圏() + 308 ", 失策=" + this.get失策() ; 309 310 } 311}
1 村上 (ヤ) .340 115 496 394 134 17 0 49 298 120 95 97 97 5 0 0 12 6 5 .476 .756 1.232 .376 12 2 大島 (中) .323 82 367 337 109 11 1 1 125 15 35 42 21 7 2 0 7 3 1 .375 .371 .746 .246 0 3 佐野 (デ) .320 101 438 406 130 26 1 17 209 57 48 44 28 3 0 1 0 1 10 .368 .515 .882 .289 4 4 宮﨑 (デ) .308 93 373 334 103 22 0 9 152 34 39 25 35 2 0 2 0 0 11 .375 .455 .830 .254 5 5 近本 (神) .293 110 485 441 129 14 1 1 148 25 58 50 35 6 3 0 23 5 7 .353 .336 .688 .314 2 6 坂倉 (広) .289 121 505 456 132 17 3 12 191 56 64 72 37 8 0 4 1 1 13 .350 .419 .769 .295 13 7 牧 (デ) .288 103 437 389 112 28 1 22 208 70 55 65 34 8 0 コード
回答1件
あなたの回答
tips
プレビュー