Adnan Ghaffar
Adnan Ghaffar

Reputation: 1423

how to skip first line when reading a csv in java

I want to skip the first col of first row and compare each element with string. Java Code

 String csvFileToRead = "C:\\Users\\Dell\\Downloads\\Tasks.csv";        
            BufferedReader br = null; 
            String[] Task = null;
            String line;  
              String splitBy = ",";
              br = new BufferedReader(new FileReader(csvFileToRead)); 
              System.out.println("Excel data for Subejct: ");
            while ((line = br.readLine()) != null) {            
                Task = line.split(splitBy); 
                System.out.println(Task[0]);    
               }  

Upvotes: 0

Views: 3787

Answers (2)

kmera
kmera

Reputation: 1745

To skip the first row just do a br.readLine(). To skip the first column start iterating the cells at 1 instead of 0.

See this example:

br.readLine(); //skips the first row
while ((line = br.readLine()) != null) {
    String[] cells = line.split(splitBy);
    //starting from 1 essentially skips the first column
    for (int col = 1; col < cells.length; col++) {
        if (cells[col].equals(someString)) {
            //do something
        }
    }
}

Upvotes: 1

Typo
Typo

Reputation: 1910

Add a br.readLine() outise the loop, for the row jump. For printing all cols but the first one do:

for(int i = 1; i < Task.lenght ; ++i){
 System.out.println(Task[i]);
}

Upvotes: 0

Related Questions