jQuery in Action, 3rd edition
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "easy array question" Watch "easy array question" New topic

easy array question

Alex Hank

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.


Ben Souther

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

Joined: Jan 10, 2002
Posts: 63874

Moved to Java in General(beginner).

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link: http://aspose.com/file-tools
subject: easy array question
It's not a secret anymore!