0

Updated Code. This program should take the CSV file and separate it into TSV files by school,but I am not getting it to work. I am getting it to create the files correctly, but only one has any data in it...

public class Student implements Comparable<Student>{

    public int id = 0;
    public String name = "";
    public String school = "";


    public Student(int id, String name, String school){

        this.id = id;
        this.name = name;
        this.school = school;
    }

    public String toString(){
        return id+"\t"+name+"\t"+school;
    }

    @Override
    public int compareTo(Student o) {
        return  this.school.compareTo(o.school);
    }
}

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


public class ReadCSV {

    public static String CSV_FILE_PATH = "/Users/eringray/Desktop/csvtotab/input.csv";

    public static void main(String[] args){

        try {

            BufferedReader br = new BufferedReader(new FileReader(CSV_FILE_PATH));
            BufferedWriter bw = new BufferedWriter(new FileWriter(CSV_FILE_PATH + ".tsv"));

            ArrayList<Student> list = new ArrayList<Student>();

            String line = "";
            while((line = br.readLine()) != null) {
                String[] values = line.split(",");

                if(values.length == 3) {
                    String idAsString = values[0];
                    String name = values[1];
                    String school = values[2];

                    int id = Integer.parseInt(idAsString);

                    Student s = new Student(id, name, school);

                    list.add(s);
                }
            }

            Collections.sort(list);

            String currentSchool = "";
            for(int i = 0; i < list.size(); i++){
                Student stu = list.get(i);
                if(currentSchool != stu.school){
                  currentSchool = stu.school; 
                  bw = new BufferedWriter(new FileWriter(CSV_FILE_PATH + stu.school + ".tsv"));
              }

              String lineText = stu.toString();
              bw.write(lineText);
              bw.newLine();
            }

            br.close();
            bw.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
5
  • Do you really need to use java for this, using the linux utility tr is quicker as it can do tr ',' '\t' to replace them really quick Commented Jan 22, 2016 at 15:28
  • yeah, I need to use java, this is an assignment I'm working on. thanks! Commented Jan 22, 2016 at 15:30
  • do you allready have some code? please post it, if so. What you need, is the replace function of the string class: docs.oracle.com/javase/7/docs/api/java/lang/… Commented Jan 22, 2016 at 15:32
  • I feel like I would need something like this, but am unsure of how to change the variables to make it do what I want to on different files. coderanch.com/t/592700/java/java/… Commented Jan 22, 2016 at 15:37
  • 1
    read my answer, but if you dont have to sort the list by school, but only replace the , by tabs, you can read the file (bufferedreader) line by line, use the replace method on the line (to replace , by \t) and write the output line in a new file (bufferedwriter). for more details, read my answer Commented Jan 22, 2016 at 15:47

1 Answer 1

2

The first thing, you have to do is reading the input file. I think, you need to read it line by line (depends on file structure).

https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html

https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html

Next step is to seperate the data and sort it by school (if i understood your question well).

For this you have to split the data and create a class to store the information:

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

public Class Student{
   public String name = "";
   ....

   public Student(String name, String school, ...){}
}

When you have created a Student object for each student in the list, you have to sort the students by school:

You could implement compareable and use Collection.sort().

https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Last thing is to print the output, for this you have to override the toString method of the student class:

public String toString(){
   return this.id+"\t"+this.name+"\t"+this.school;
}

and iterate throug the list of your students and call the toString method:

System.out.println(students.get(i).toString());

EDIT:

If you need the output in a file and not in the console, just use a fileoutputStream and a bufferedwriter to print the output of the toString method in a file.

Sign up to request clarification or add additional context in comments.

16 Comments

Awesome! That is exactly what I was looking for! I'll give that a shot and may return with a few small questions. Thanks again!
I've been working on this over the weekend but have been struggling. I've added what I have so far to my original post. For the next step, am I supposed to create a new java class file? How could I get that to work with my original code? Sorry for the novice questions, I just started learning java last week, so I am quite behind!
the assignment is to do it in a text editor, but I don't really understand how to do it that way
ok, i will write you same code and explanaition (should take about 10 mins)
@Rassisland so, i have written some code, with comments. At first, create a folder, for your code, then create to text files and name them: Person.java and Test35.java ---- you can change the names, but then you should also change the class names. The code is here: pastebin.com/aYJj1AQk and pastebin.com/avC7vqzx The last think you need, is to change path to your file and then compile the whole think: go on commandline into the folder, with your java files and do javac *.java to compile all the files and java Test35 to run the main method of the programm.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.