0

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".

  • don't make a new CsvReader in the for loops, reuse the already open csvReader that you got the header line from... – Dru Aug 18 '13 at 03:28
  • i did that originally to skip the first row with the headers so the code would grab whatever was in the following rows. i removed the Csvreader in the loops, but there seems to be no difference in the outcome... – user2499705 Aug 18 '13 at 04:46

3 Answers3

0

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Then you have other bugs. Edit your question and paste your updated code, after the changes suggested in the comments and in my answer. Show example input, the output you desire, and the ourput you get. – JB Nizet Aug 20 '13 at 07:29
0

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
baskar_p
  • 665
  • 5
  • 16
0

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.

cmd
  • 11,622
  • 7
  • 51
  • 61