Question

I have CSV files with headers and specific numbers. I need to extract those numbers individually to create a AIM XML file with, but that's not important. I'm having a hard time trying to extract the numbers based on the their headers. I've tried various things but couldn't get it to work. When I try to run this code (below is just a sampling), it never seems to terminate. I'm not very experienced with Java or OpenCSV, so please excuse the mess you are about to see. (it's also 2AM and my mind is malfunctioning)

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import edu.stanford.hakan.aim3api.base.AimException;
import edu.stanford.hakan.aim3api.base.ImageAnnotation;
import edu.stanford.hakan.aim3api.usage.AnnotationBuilder;
import au.com.bytecode.opencsv.CSVReader;



public class CreateAIMFile {
public static void main(String[] args) throws IOException, FileNotFoundException,     AimException
{
    ImageAnnotation PeserAnnotation1 = new ImageAnnotation ();
    String csvFilename = "C:/Users/Daniela/Documents/ISPY/205_4493_MR1_ACRIN_6657/E25008/peser_data.csv"; //Insert file name and location
    CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
    String [] nextLine = csvReader.readNext();

    //start 
    for (int i = 0; i<50; i++){ //i<50 is a random number
        while (nextLine[i] != null){
            if (nextLine[i] == "CagridId") //customize name
            {
                CSVReader csvreader = new CSVReader(new FileReader(csvFilename), ';', '\'', 1);
                Integer A = Integer.valueOf(nextLine[i]);
                PeserAnnotation1.setCagridId(A); 
            }
            else
            {
            PeserAnnotation1.setCagridId(0); 
            }
        }
    }
    for (int i = 0; i<50; i++){ 
        while (nextLine[i] != null){
            if (nextLine[i] == "PatientComment") //customize name
            {
                CSVReader csvreader = new CSVReader(new FileReader(csvFilename), ';', '\'', 1);
                PeserAnnotation1.setComment(nextLine[i]); 
            }
        else
            {
                PeserAnnotation1.setComment("");
            }
        }
    }
AnnotationBuilder.saveToFile(PeserAnnotation1, "./AIMFileName.xml", "AIM_v3.xsd");     //customize name of XML file
    System.out.println(AnnotationBuilder.getAimXMLsaveResult());
}
}
Was it helpful?

Solution 2

I think you are confusing csvReader.readNext(); with csvReader.readAll();

Try this (not compiled!):

String headerLine = csvReader.readNext();

List numbersInHeader = new ArrayList();
for(int i = 0 ; i < headerLine.length ; i++){
    if(headerLine[i].equals("YouHeaderNameHere") || headerLine[i].equals("OtherNumberHeader")){
        numberInHeader.add(i);
    } 
}
String line[] = null;
while((line = csvReader.readNext()) != null){
    for(int i = 0 ; i < line.length ; i++){
        if(numbersInHeader.contains(i)){ 
            // This is a number do you work here

        }

    }
}

Hope this helps.

OTHER TIPS

Usually one will open the CSV file once by instantiating a CSVReader object and then loop through the file calling readNext.

In your code you are re-instantiating the CSVReader in the loop and not calling readNext in your loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top