wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes easy array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "easy array question" Watch "easy array question" New topic
Author

easy array question

Alex Hank
Greenhorn

Joined: Apr 27, 2005
Posts: 16
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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
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.


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Moved to Java in General(beginner).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
jQuery in Action, 2nd edition
 
subject: easy array question