0

I am populating a list array with lines from a csv file. I have some empty columns and some not. I would like these empty columns to contain "0" if they are empty. The length of the string[] could be 1 to 7. However, I need to return 7 items when all is said and done.

My data looks like this:

Brenda Mines Snow Pillow,2013-11-26 04:00:00,-2.943,364,59,,

or this:

Barnes Creek Snow Pillow,2013-11-26 04:00:00,-6.6,344,117,10,12.97

or possible other variations missing other columns.

How do I always make my final String[] data; have 7 items in it?

data[0] through data[6]

I have tried an if(data[i] == null){data[i] = "0";} but that doesn't work since my list from the parsed data may not have a length of i and I get a index OB error.

I tried creating another array and adding the items in there and then if an item from data[i] was null, data2[i] = "0" else data[i] = data2[i]; However, data[i] might not exists... I am just confused on how to make my array always contain 7 items.

Any help would be so great. I should add that I have tried to initialize first: String[] data = {"0","0","0","0","0","0","0"}

EDIT: I checked to make sure data.length was 7... it was, so what the problem was: NULL vs empty string is answered here.

Community
  • 1
  • 1
jasonflaherty
  • 1,924
  • 7
  • 40
  • 82

2 Answers2

1

When you create the array, you could initialize it to an array of size 7 of all 0's. Then, you could replace the data you read in with the data from your .CSV file, and anything that was "empty" will have your default, desired value of 0.

Alex
  • 1,100
  • 1
  • 9
  • 23
  • Hi Rhino, I have tried that multiple ways... maybe I am missing something. String[] data = new String[7] and String[] data = {"0","0","0","0","0","0","0"} did not add 0 when the col was empty... Any thoughts on why not? I figured that would work for sure. – jasonflaherty Nov 26 '13 at 06:45
  • hm, maybe I'm not understanding right? So when you read in a column with no value (`,,`) your reader is completely skipping over it? – Alex Nov 26 '13 at 06:48
  • No, I don't think so. If I choose a random spot in my array: String[] dt = results.get(randomnumber); and get the length, it is 7. Just as it should be. However, there might not be data in one of those spots. I think that is where my error is. – jasonflaherty Nov 26 '13 at 06:56
  • Then I'm possibly confused on how you are storing your data, because if you initialize the array to be {"0", "0", "0", "0", "0", "0", "0"}, none of those spots should ever NOT have data. – Alex Nov 26 '13 at 15:28
  • Thanks for the comments and ideas. That is exactly what I was thinking... This ended up working: if (data[0] != "") rather than if (data[0] != null)... I guess "" != null and since the data is in String format. – jasonflaherty Nov 26 '13 at 16:26
  • Glad you were able to find your answer! And +1 for Editing your answer to contain the solution you found. It may really help someone some day. – Alex Nov 26 '13 at 16:59
0

The problem I ran into is that NULL is different than "" when it comes to Strings.

The NULL is special data type, it means absence of value.

An empty string on the other hand means a string or value which is empty.

From: NULL vs empty string

Community
  • 1
  • 1
jasonflaherty
  • 1,924
  • 7
  • 40
  • 82