Question

I'm trying to get whatever is under a specific header and turn that into a String. I want my code to recognize a header and get the content right under it. Basically get info in a specific cell. What am I doing wrong? I know how to do this by calling specific columns, but now I want to know how to do it by making the code recognize a specific header.

Edit: I updated my code, but still nothing is changed... Help?

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 [] headerLine = csvReader.readNext();

    //start imageannotation info
    for (int i = 0; i < headerLine.length; i++){
            if (headerLine[i].equals("PRIMARY_ID")) //customize name
            {
                CSVReader csvreader = new CSVReader(new FileReader(csvFilename), ',', '\'', 1);
                String [] nextLine = csvreader.readNext();
                PeserAnnotation1.setPRIMARY_ID(nextLine[i]); 
            }
            else
            {
                PeserAnnotation1.setPRIMARY_ID(""); 
            }
    }
    for (int i = 0; i < headerLine.length; i++){    
            if (headerLine[i].equals("ISPY_ID")) //customize name
            {
                CSVReader csvreader = new CSVReader(new FileReader(csvFilename), ',', '\'', 1);
                String [] nextLine = csvreader.readNext();
                PeserAnnotation1.setISPY_ID(nextLine[i]); 
            }
        else
            {
                PeserAnnotation1.setISPY_ID("");
            }
    }

The CSV File:

PRIMARY_ID,ISPY_ID, DATE, PE_FTV
205, 1207, 20050819, 8.7508545

Both my code and the CSV file are just samples of the real thing. Currently the code just skips over the "if" and goes straight to the "else".

Was it helpful?

Solution 3

To fix the problem, I inserted a break after the if statement. Without it, the code would enter the loop again, enter the else clause, and replace the string from the if statement with the stuff in the else.

OTHER TIPS

You're using == to compare Strings. Use equals() instead. == tests if two expressions reference the exact same object. Not if two strings contain the same characters.

Your code in fact goes inside the if condition.

for (int i = 0; i < headerLine.length; i++){
            if (headerLine[i].equals("PRIMARY_ID")) //customize name
            {
                System.out.println("inside PRIMARY_ID IF");
                CSVReader csvreader = new CSVReader(new FileReader(csvFilename), ',', '\'', 1);
                String [] nextLine = csvreader.readNext();

            }
            else
            {
                System.out.println("inside PRIMARY_ID ELSE");
            }
    }
    for (int i = 0; i < headerLine.length; i++){    
            if (headerLine[i].equals("ISPY_ID")) //customize name
            {
                System.out.println("inside ISPY_ID IF");
                CSVReader csvreader = new CSVReader(new FileReader(csvFilename), ',', '\'', 1);
                String [] nextLine = csvreader.readNext();

            }
        else
            {
            System.out.println("inside ISPY_ID ELSE");
            }
    }

gives me the following output

inside PRIMARY_ID IF
inside PRIMARY_ID ELSE
inside PRIMARY_ID ELSE
inside PRIMARY_ID ELSE
inside ISPY_ID ELSE
inside ISPY_ID IF
inside ISPY_ID ELSE
inside ISPY_ID ELSE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top