Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

easy array question

 
Alex Hank
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some simple code that blows up if the string array is empty. I need an if statement that will not execute the code if the array is empty.


int numDir = 0;

String [] selfiles = request.getParameterValues("selfile");

for(int i = 0; i < selfiles.length; i++){ if(selfiles[i].indexOf(".") == -1 ){numDir++;} }//ELEMENTS WITHOUT "." ARE NOT FOLDERS


I need an if statement that will not run the for loop if the array selfiles is empty.

thanks

Alex
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming you mean null.
That for loop shouldn't blow up if you have a zero length array.

[ June 21, 2005: Message edited by: Ben Souther ]
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A look at the code suggests that it should cope fine when the array is empty, ie a zero sized array.

However I suspect your problem is when the array variable is null. So the check you would want is



Someone will also probably tell you this was not the right place to post it, as it has little to do with JSP and is more of a java beginners question.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General(beginner).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic