質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

Q&A

解決済

2回答

17849閲覧

javaでの、CSVファイルで読み込んだデータを整理して、別のCSVファイルに出力したい

退会済みユーザー

退会済みユーザー

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

0グッド

0クリップ

投稿2016/02/13 09:20

test.csvから成績データを読み取って、
seiseki.csvに出力します。

(test.csvの例)
出席番号 氏名 数学 英語 国語 理科 社会
1 A 77 80 - 45 83
2 B - 71 66 69 97
3 C 87 89 80 87 55
4 D 55 89 - 59 100

(seiseki.csvの例)
出席番号 氏名 数学
3 C 87
1 A 77
4 D 55

出席番号 氏名 英語
3 C 89
4 D 89
1 A 80
2 B 71

出席番号 氏名 国語
3 C 80
2 B 66

出席番号 氏名 理科




出力は5教科分のデータを、英語や国語のように出力する。
・欠席者(-)は集計から除外する
・点数の高い人から順に並べ替えて記述する。
・同点の場合、出席番号の小さい順に並べ替えて記述。

以下に私の途中経過として、読み込んだデータを配列に格納して、
入力したままをcsvファイルに書き出すプログラムを書いております。
どのように書き足していけば、いいでしょうか?
よろしくお願いします。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReadWrite {
public static void main(String[] args) {
String line;
String[] csvArray = null;
File inputFile = new File("C:\test.csv");
File outputFile = new File("C:\seiseki.csv");
BufferedReader in = null;
BufferedWriter out = null;
try {
in = new BufferedReader(new FileReader(inputFile));
out = new BufferedWriter(new FileWriter(outputFile));

/** 1行ずつ読み込んで整形する */ while ((line = in.readLine()) != null) { // 区切り文字カンマ[,]で、区切られた文字列を配列に格納する csvArray = line.split("\\,"); // データを出力する for (int i = 0; i < csvArray.length; i++) { // 行の最後のデータはカンマ付けない if (i != csvArray.length - 1) { out.write(csvArray[i]); out.write(","); } else { out.write(csvArray[i]); } } // 改行する out.newLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

下記のような処理になりそうですね。
1-フィールドに出席番号、氏名、点数を持つクラスを作成。
2-各教科毎に1の型を格納するリストを作成する。
3-test.csvから1行ずつ読みだし、1で作ったクラスのインスタンスを各教科分作成する。
4-3で作った各教科のインスタンスを2で作ったリストに振り分ける。この時点数が無いものは追加しない。
5-各教科毎に振り分けたリストを出力したい順にソートする。
6-5のリストをseiseki.csvに出力する。

投稿2016/02/13 10:52

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReadWrite {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
String line;
String[] csvArray = null;
File inputFile = new File("./test.txt");
File outputFile = new File("./seiseki.txt");
BufferedReader in = null;
BufferedWriter out = null;
try {
in = new BufferedReader(new FileReader(inputFile));
out = new BufferedWriter(new FileWriter(outputFile));
/** 1行ずつ読み込んで整形する */
while ((line = in.readLine()) != null) {
// 区切り文字カンマ[,]で、区切られた文字列を配列に格納する
csvArray = line.split("\,");
// ここから
String id = csvArray[0];
String name = csvArray[1];
Student student = new Student(id, name);
student.setMath(Integer.parseInt(csvArray[2]));
student.setEnglish(Integer.parseInt(csvArray[3]));
student.setScience(Integer.parseInt(csvArray[4]));
list.add(student);
}
print(out, list, Student.MATH);
print(out, list, Student.ENGLISH);
print(out, list, Student.SCIENCE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

private static void print(BufferedWriter out, List<Student> list, int subject) throws IOException { // TODO Auto-generated method stub Student.setSortSubject(subject); Collections.sort(list); out.write("番号,名前," + Student.getSubjectName(subject)); out.newLine(); for (Student student : list) { String str = student.getID(); str += " " + student.getName(); str += " " + student.getScore(subject); out.write(str); out.newLine(); } }

}

class Student implements Comparable<Student> {
public final static int TOTAL = 0;
public final static int MATH = 1;
public final static int ENGLISH = 2;
public final static int SCIENCE = 3;
public final static String[] SUBJECTNAME = { "total", "math", "english",
"science" };
private static int SORTSUBJECT = TOTAL;
private String id;
private String name;
private int math;
private int english;
private int science;

public Student(String id, String name) { this.id = id; this.name = name; this.math = 0; this.english = 0; this.science = 0; } public static String getSubjectName(int num) { return SUBJECTNAME[num]; } public int getScore(int subject) { int score = 0; switch (subject) { case MATH: score = this.math; break; case ENGLISH: score = this.english; break; case SCIENCE: score = this.science; break; case TOTAL: score = this.math + this.english + science; break; } return score; } public static void setSortSubject(int subject) { SORTSUBJECT = subject; } public void setMath(int score) { this.math = score; } public void setEnglish(int score) { this.english = score; } public void setScience(int score) { this.science = score; } public String getID() { return this.id; } public String getName() { return this.name; } public int getMath() { return this.math; } public int getEnglish() { return this.english; } public int getScience() { return this.science; } @Override public int compareTo(Student o) { // TODO Auto-generated method stub int value = 0; switch (SORTSUBJECT) { case MATH: value = compareTo(this.math, o.getMath()); break; case ENGLISH: value = compareTo(this.english, o.getEnglish()); break; case SCIENCE: value = compareTo(this.science, o.getScience()); break; case TOTAL: int a = this.math + this.english + this.science; int b = o.getMath() + o.getEnglish() + o.getScience(); value = compareTo(a, b); break; } return value; } private int compareTo(int a, int b) { // TODO Auto-generated method stub if (a < b) { return 1; } else if (a > b) { return -1; } return 0; }

}

投稿2016/02/13 11:49

AqueductAD1950

総合スコア30

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2016/02/13 12:27

回答ありがとうございます。 合計点についてですが、 例では数学、理科、英語が 全員受験していますが、 その例を変えても対応できるような、 プログラムを教えていただけますか?
AqueductAD1950

2016/02/13 13:43 編集

欠席者(-)は集計から除外する 点の質問ということでよろしいですか? 欠席した場合には、点数が"-"となるので、set~で欠席したと判断できる値(たとえば-1)を設定します。そして、出力時に点数が-1であれば出力しないように書き換えれば良いと思います。 値取得時 if(csvArray[i].equals("-")){ student.set~(-1); }else{ student.set~(Integer.parseInt(csvArray[i])); } 出力時 if(student.getScore(subject) != -1){ String str = student.getID(); str += " " + student.getName(); str += " " + student.getScore(subject); out.write(str); out.newLine(); } 捕捉 [同点の場合、出席番号の小さい順に並べ替えて記述] に関して実装していませんでした。 この点は、public int compareTo(Student o) での点数の比較結果が同じ(compareTo(int a, int b)の戻り値が0)である場合に、学生番号の比較(String.compareTo()の戻り値を返す)を行えば解消できると思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問