user3615820
user3615820

Reputation: 43

Empty lines not getting printing in reading CSV files in java

I have following code in which i m reading a csv file in java,The csv file is as follows

"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"

Following code for reading file in java

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


public class CSVFileReader {

  public static void main(String[] args) {

    CSVFileReader obj = new CSVFileReader();
    obj.run();

  }

  public void run() {

    String csvFile = "Whois.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

                // use comma as separator
                String[] country = line.split(cvsSplitBy);
                if(line.contains("AU") || line.contains("AU"))
                {

                         System.out.println("Country [code= " + country[4] 
                                 + " , name=" + country[5] + "]");
                }
                else
                {
                        System.out.println("");
                }

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("Done");
  }

}

With this java code I am reading the lines where i m getting AU in line.it showing correctly two results which is correct,But i want like where ever i m not getting AU string in line.It should print that row as blank.

As we can see in csv file code.That AU is in first and third line,its printing two lines where AU is present.I want the output to be first AU row then second row should be blank and then in third row the required AU values...

How can i print the second empty row as well,right now its printing two rows which contains AU in line

Upvotes: 0

Views: 810

Answers (1)

Norbert Radyk
Norbert Radyk

Reputation: 2618

To write an empty line for non AU lines you can write:

String[] country = line.split(cvsSplitBy);
if( line.contains("AU") ) {
   System.out.println("Country [code= " + country[4]  + " , name=" + country[5] + "]");
} else {
   System.out.println("");
}

Though this approach is not an example of a good coding/design (maybe except the case of simplest, single use scripts, where it doesn't really matter).

I'd suggest thinking about separating processing of the CSV results (in the form of method which will take in the CSV file and return the collection of processed lines/results) and printing them out (take in the collection of lines/results and iterate over to print them).

This way you would be able to test in an automated manner that you've processed the results correctly.

Upvotes: 1

Related Questions