0

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());
            } 

        }
    }
3
  • Try to change cities = dataContent[0]; to cities = dataContent[i]; Commented Jan 16, 2020 at 9:01
  • cities = dataContent[i]; would print the whole file non ? Commented Jan 16, 2020 at 9:05
  • Maybe your csv fields are separated by a ',' or something else than ';'? Commented Jan 16, 2020 at 9:10

3 Answers 3

1

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);
            }          
Sign up to request clarification or add additional context in comments.

1 Comment

It works thank you very much, I also had to change the separator as suggested @JoakimDanielson. Thank you to you both
0

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());
                }


            }
        }

Comments

0

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());
        }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.