Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

I was trying to get the record which contains null word between comma from text file and printing to log file

for example :

paris,, America, Spain, Japan
india,Srilanka,Pakistan,Bangladesh

I will get the output in log: Paris, ,America, Spain, because getting null between comma below is my code .

from code i dont understand why array not detecting null eventhough i have split with (,).
pls help me out .

What I have tried:

while((s = sr.readLine())!= null){
PrintStream p = new PrintStream(Log_file);
String a[]= s.split(",");
// System.out.println(s);
//System.out.println(a[5]);
for(String k:a)
{
if(k !=null)
{
//System.out.println(a[i]);
if(a[i] ==null){
p.println(s);
Posted
Updated 19-Jan-18 21:55pm

1 solution

There won't be null values in a. If there is nothing between two commas, the result in a will be an empty string rather than null. Instead of checking for null, check if the string is empty, with k.isEmpty().
 
Share this answer
 
Comments
devfoot 20-Jan-18 6:58am    
thanks its working , but i have one more situation here paris,america
in this case if america is missing then split function not taking last word ,any idea ?
Thomas Daniels 20-Jan-18 7:09am    
I'm not sure what you mean.
devfoot 20-Jan-18 8:29am    
if first word and last is empty then my code is failing
ex: ,paris,america,
before paris after america the word is empty then how i can catch that, i was doing regression testing and applied test case so my code is failing here .
Thomas Daniels 20-Jan-18 8:30am    
Well, what will happen, is just that the first and the last element in the array will be empty strings. Knowing that, you can adjust your code to fix the problem. I don't know what you're trying to do, what the expected results are and what the actual results are, but you do know that, so you can use a debugger to figure out what goes wrong.
devfoot 25-Jan-18 4:03am    
sorry for late reply : my question is : if ,paris,london,japan will be in record .
then my code is not taking this as empty record .
code is checking only between , and , .
its not taking if first letter itself is blank .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900