Java 6일차 File, Vector, Scanner 테스트

/* 파일 불러와서 처리 (학생 성적 총점, 평균 계산) */

Student.java

class Student
{
int number;
String name;
int score1;
int score2;
int score3;
int sum;
double avg;

Student(int num, String n, int s1, int s2, int s3)
{
number = num;
name = n;
score1 = s1;
score2 = s2;
score3 = s3;
sum = s1+s2+s3;
avg = sum/3.0;
}

void Print()
{
System.out.println(number+" "+name+'\t'
+score1+" "+score2+" "+score3+" "+sum+" "
+Double.parseDouble(String.format("%.1f", avg)));
}
}

FileTest.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;

public class FileTest
{
public static void main(String args[])
{
Vector<Student> vs = new Vector<>();

try
{
Scanner sc = new Scanner(new File("C:\\java\\workspace\\score.txt"));

while(sc.hasNextLine())
{
int num = sc.nextInt();
String n = sc.next();
int s1 = sc.nextInt();
int s2 = sc.nextInt();
int s3 = sc.nextInt();
vs.add(new Student(num, n, s1, s2, s3));
}
}
catch(FileNotFoundException e)
{
System.out.println("Exception");
}

for(int i=0; i<vs.size(); i++)
{
vs.get(i).Print();
}
}
}

score.txt

1 King 87 75 90
2 Jake 90 64 99
3 Coco 67 86 65
4 Kim 95 92 84
5 Tasha 77 90 95

댓글 없음: