Pawan
Pawan

Reputation: 32321

How to Check for empty line while parsing csv file?

From third party daily reports i will be getting a similar kind of csv file as shown below

07-Jan-2016
It is better to lead from behind and to put others in front, especially when you celebrate victory when nice things occur. You take the front line when there is danger. Then people will appreciate your leadership.

The main thing that you have to remember on this journey is, just be nice to everyone and always smile.

My requirement is that i need to put each paragraph (A line after space) each quote for above reference in a separate StringBuffer

My question how can i check for empty line ??

I tried with

if(line.contains("            "))
{
System.out.println("hjjjjjjjjjjjjjjjjk");
}

But the above is causing issue where ever there is a space

I am reading the csv file as shown below

 String csvFile = "ip/ASRER070116.csv";
  BufferedReader br = null;
  String line = "";
  try {
   br = new BufferedReader(new FileReader(csvFile));
   while ((line = br.readLine()) != null) {
    if (line.startsWith(",")) {
     line = line.replaceFirst(",", "");
    }
    System.out.println(line);
   }
  } 

Could you please tell me how to resolve this ??

Upvotes: 0

Views: 3614

Answers (3)

Tobías
Tobías

Reputation: 6297

You can use StringUtils.isBlank method from Apache Commons Lang

public static boolean isBlank(CharSequence cs)

Checks if a CharSequence is whitespace, empty ("") or null.

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("bob")     = false
 StringUtils.isBlank("  bob  ") = false

Upvotes: 0

Danail Alexiev
Danail Alexiev

Reputation: 7772

I would suggest you use trim() on the read line and then check if line.length == 0

Upvotes: 1

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5636

if (line != null && (line.trim().equals("") || line.trim().equals("\n"))){
   System.out.println("this is empty line");
}

Upvotes: 1

Related Questions