Marvin K. Langlois
Marvin K. Langlois

Reputation: 23

How print data from a CSV after a split using Java fileReader?

I'm trying to work on a csv file with Java.

After a split, I would like to work on the first column of the file. But when I print my variable (cities = dataContent[0]) I still have all the file in the console.

package klm.java.controlFile;

import java.io.*;

    public  class ControleCSV {
        public static void main(String[] args) {
            try{
                FileReader fr = new FileReader("communes_avec_erreurs.csv");
                BufferedReader br = new BufferedReader(fr);

                String dataContent[];
                String cities;
                StringBuilder lsbContenu = new StringBuilder();
                String lsLigne; 

                while ((lsLigne = br.readLine()) != null) {
                    dataContent = lsLigne.split(";");
                    for(int i = 0; i < dataContent.length; i++){
                         lsbContenu.append(dataContent[i]);
                         lsbContenu.append("\n");

                         cities = dataContent[0];

                        System.out.println(cities);
                    }
                }

                br.close();
                fr.close();

             // System.out.println(lsbContenu.toString());

            } catch (FileNotFoundException e) {
                System.err.println("Erreur de fichier : " + e.getMessage());
            } catch (IOException e) {
                System.err.println("Erreur de lecture : " + e.getMessage());
            } 

        }
    }

Upvotes: 0

Views: 59

Answers (3)

Shamil Puthukkot
Shamil Puthukkot

Reputation: 492

Check this out

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

public class ControleCSV  {
    public static void main(String[] args) {
        try {
            FileReader fr = new FileReader("zone.csv");
            BufferedReader br = new BufferedReader(fr);
            String dataContent[];
            String cities;
            String lsLigne;
            while ((lsLigne = br.readLine()) != null) {
                dataContent = lsLigne.split(",");
                cities = dataContent[0];
                System.out.println(cities);
            }

            br.close();
            fr.close();

        } catch (FileNotFoundException e) {
            System.err.println("File not found " + e.getMessage());
        } catch (IOException e) {
            System.err.println("Erreur de lecture : " + e.getMessage());
        }

    }
}

Upvotes: 0

Pandit Biradar
Pandit Biradar

Reputation: 1877

I think you are looking for the first line to print here the problem with while loop , this loop is reading each line and dataContent is replace with new line on each while loop read , so each it time it has only 1 index data in dataContent array

while ((lsLigne = br.readLine()) != null) {
 dataContent = lsLigne.split(";");

I feel felow sample code can help put one counter if its lines ==1 then print 0 datapoint else if you want to write extra code you can

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

        public class Test {

            public static void main(String[] args) {


                try {
                    FileReader fr = new FileReader("communes_avec_erreurs.csv");
                    BufferedReader br = new BufferedReader(fr);

                    String dataContent[];
                    String cities;
                    StringBuilder lsbContenu = new StringBuilder();
                    String lsLigne;
                    int lines = 0;

                    while ((lsLigne = br.readLine()) != null) {
                        lines++;
                        dataContent = lsLigne.split(";");
                        if (lines == 1) {
                            lsbContenu.append(dataContent);
                            lsbContenu.append("\n");
                            cities = dataContent[0];
                            System.out.println(cities);
                        } else {
//TODO your else logic
         }
                    }


                    br.close();
                    fr.close();

                    // System.out.println(lsbContenu.toString());


                } catch (FileNotFoundException e) {
                    System.err.println("Erreur de fichier : " + e.getMessage());
                } catch (IOException e) {
                    System.err.println("Erreur de lecture : " + e.getMessage());
                }


            }
        }

Upvotes: 0

Genc
Genc

Reputation: 11

Move this part outside the for-loop, but keep it still in the while-loop:

cities = dataContent[0];
System.out.println(cities);

Like this:

           while ((lsLigne = br.readLine()) != null) {
                  dataContent = lsLigne.split(";");
                  for(int i = 0; i < dataContent.length; i++){
                       lsbContenu.append(dataContent[i]);
                       lsbContenu.append("\n");
                  }
                  cities = dataContent[0];
                  System.out.println(cities);
            }          

Upvotes: 1

Related Questions