We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Empty element in array of strings
Page Index Toggle Pages: 1
Empty element in array of strings? (Read 1205 times)
Empty element in array of strings?
Apr 24th, 2009, 2:29pm
 
Hi,

Say you have a tab-delimited file:

a TAB b TAB c
d TAB e TAB
f TAB g TAB h

note that the middle line has no value in the last element.

If I load this into an array of strings:
String foo[] = split(myfile, TAB);

The array for each line has a length of 3. The third element in the middle line shows up as a blank. I, however, don't know how to test for it. I have tried null, and "" and " " to no avail. Can anyone tell me how to test for this blank element?
Re: Empty element in array of strings?
Reply #1 - Apr 24th, 2009, 2:57pm
 
Test for foo.length

Arrays in Java have that built in. Smiley
Re: Empty element in array of strings?
Reply #2 - Apr 24th, 2009, 3:04pm
 
I have. They are all the same length.
If I print out the array values I will see

[0] "d"
[1] "e"
[2] ""

But testing for "" or null doesn't seem to work.



NoahBuddy wrote on Apr 24th, 2009, 2:57pm:
Test for foo.length

Arrays in Java have that built in. Smiley

Re: Empty element in array of strings?
Reply #3 - Apr 25th, 2009, 1:11am
 
You can test if a string is empty with either:

str.length() == 0

or

str.equals("")
Re: Empty element in array of strings?
Reply #4 - Apr 25th, 2009, 7:25pm
 
Oh, that is brilliant. Thanks so much! You saved me some hair Smiley

PhiLho  wrote on Apr 25th, 2009, 1:11am:
You can test if a string is empty with either:

str.length() == 0

or

str.equals("")

Re: Empty element in array of strings?
Reply #5 - Apr 26th, 2009, 1:42am
 
Two gentle remarks about general forum etiquette: not need to quote somebody just above your reply, unless you want to isolate a specific part of a long message, and avoid top-posting:
Quote:
Please do not top post. By this, we mean that if you are replying to a message, please put your replies after the text that you copy in your reply.
   * A: Because it reverses the logical flow of conversation.
   * Q: Why is top posting frowned upon
Mailing List Etiquette applies also to forums... Smiley

Just a reminder, to avoid a trap lot of new comers to Java fall into: don't compare strings with ==, always use equals().
Page Index Toggle Pages: 1