简体   繁体   中英

java class to read csv files instead of text files

The task is to create a java program that reads information from three .csv files and output a list of transcripts, ordered in descending order of aggregate mark, to a file in the current directory called "RankedList.txt". The program should show whether students have passed their year at university and what grade they achieved. The students took two modules, IR101 and IR102. This data is stored in two .csv files, IR101.csv and IR102.csv. Their names and registration numbers are stored in students.csv. The rules of assessment stipulate the following:

  • Students must pass both modules in order to proceed to Stage 2. The pass mark for a module is 40.
  • Students who do not pass both modules will be deemed to have failed.
  • Students who fail only one of the two modules will be allowed a resit attempt.
  • Students who fail both modules will be required to repeat the year.
  • Students who pass both modules will be awarded a class based on their aggregate mark using the following scale:

    70 – 100 = 1st

    60 – 69.9 = 2.1

    50 – 59.9 = 2.2

    40 – 49.9 = 3rd

I have been able to complete this task however one problem I have faced is that my code only works for .txt files. If someone could show me how to change my code to work with .csv files I would be most grateful. My program so far is as follows:

package assignment;

import java.io.*;
import java.util.*;

public class StudentsMarks {

public static void main(String[] args) throws FileNotFoundException,IOException {
    String currDir = "C:\\Users\\phili_000.Philip.001\\workspace\\ce152\\src\\ass\\StudentsMarks.java";


    Scanner sc = new Scanner(new File(currDir+"IRStudents.csv"));
    HashMap<Integer, String> students = new HashMap<Integer, String>();
    while (sc.hasNext()) {
        String line = sc.nextLine();
        students.put(sc.nextInt(), sc.next()); 
        String[] parts = line.split(",");
    }

    sc = new Scanner(new File(currDir+"IR101.csv"));
    HashMap<Integer, Double> ir1 = new HashMap<Integer, Double>();
    while (sc.hasNext()) {
        String line = sc.nextLine();
        ir1.put(sc.nextInt(), sc.nextDouble());
        String[] parts = line.split(",");

    }

    sc = new Scanner(new File(currDir+"IR102.csv"));
    HashMap<Integer, Double> ir2 = new HashMap<Integer, Double>();
    while (sc.hasNext()) {
        String line = sc.nextLine();
        ir2.put(sc.nextInt(), sc.nextDouble());
        String[] parts = line.split(",");
    }

    File output=new File(currDir+"RankedList.txt");
    BufferedWriter b=new BufferedWriter(new FileWriter(output));
    Iterator<Integer> ids = students.keySet().iterator();
    while (ids.hasNext()) {
        Integer id=ids.next();
        b.write(id+"  "+students.get(id));
        b.newLine();
        Double marks1=ir1.get(id);
        Double marks2=ir2.get(id);
        Double aggregate=(marks1+marks2)/2;
        b.write("IR101\t "+marks1+"\t IR102\t "+marks2+"\t Aggregate   "+aggregate);
        b.newLine();
        String classStd;
        if(aggregate>=70){
            classStd="1st";
        }else if(aggregate>=60){
            classStd="2.1";
        }else if(aggregate>=50){
            classStd="2.2";
        }else if(aggregate>=40){
            classStd="3rd";
        }else{
            classStd="failed";
        }

        String outcome;
        if(marks1<40 && marks2<40){
            outcome="Repeat the year";
        }else if(marks1<40){
            outcome="Resit IR101";
        }else if(marks2<40){
            outcome="Resit IR102";
        }else{
            outcome="Proceed to Stage 2";
        }
        b.write("Class:\t " + classStd + "\t Outcome: " + outcome);
        b.newLine();
        b.write("----------------------------------------------------");
        b.newLine();
    }
    b.flush();
    b.close();

}
}
String csvFile = "path.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

                // use comma as separator
            String[] parts = line.split(cvsSplitBy);
}

    } catch (Exception e) {
        e.printStackTrace();
    }

when reading a csv you should read the file line by line at the same time you should split the string in the line by using split method then you will get an array of strings .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM