Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

array bounds

Thread view: 
Mark - 27 Nov 2005 01:05 GMT
Scanner scan = new Scanner(System.in);
String cmd[] = scan.nextLine().split("\\s");

if( cmd[0].equals("use") )
{
   if( cmd[1] != null )
       // do some crap
   else
      // do something else
}

so basically, if the user just types "use" it will "do something else",
but if he enters a parameter... it will.. use it.

but just by calling if( cmd[1] != null ) if a 2nd parameter isnt
given.. and throws an error..

wait.. that's it isnt it? i should just catch it, instead of checking
if its null?
Lee Weiner - 27 Nov 2005 01:36 GMT
>Scanner scan = new Scanner(System.in);
>String cmd[] = scan.nextLine().split("\\s");
[quoted text clipped - 15 lines]
>wait.. that's it isnt it? i should just catch it, instead of checking
>if its null?

No, you should check the length of the array returned from split() to see how
many parameters were passed.

Lee Weiner
lee AT leeweiner DOT org
Mark - 27 Nov 2005 01:43 GMT
lol.. why didnt i think of that?

but thats... well. i was going to say more annoying but not really.

oh well. my method works well enough.

though somehow deliberately relying on throwing exceptions.. not as an
error, but as an "else"... doesnt sound like proper coding.

anyways, thanks.
zero - 27 Nov 2005 12:35 GMT
"Mark" <mnbayazit@gmail.com> wrote in news:1133055788.702467.301260
@f14g2000cwb.googlegroups.com:

> though somehow deliberately relying on throwing exceptions.. not as an
> error, but as an "else"... doesnt sound like proper coding.

It isn't - exceptions should be exceptional, not the rule.  There is a lot
of overhead involved with exceptions (even more if you have finally), so
you should avoid using them for normal program flow.

Signature

Beware the False Authority Syndrome

Roedy Green - 27 Nov 2005 06:16 GMT
>wait.. that's it isnt it? i should just catch it, instead of checking
>if its null?

you probably want:

if ( cmd.length > 1 && cmd[1] != null  && cmd[1].length() > 0 )
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 27 Nov 2005 14:10 GMT
>>wait.. that's it isnt it? i should just catch it, instead of checking
>>if its null?
>
> you probably want:
>
> if ( cmd.length > 1 && cmd[1] != null  && cmd[1].length() > 0 )

String/Pattern.split does not return arrays containing nulls. So the
null check is misleading.

For me str.length() != 0 (or !str.equals("")) is clearer than
str.length() > 0. In 1.6 it can be replaced by !str.isEmpty(). My guess
in this case is that an empty string might point up some input sequences
not considered.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 27 Nov 2005 18:11 GMT
On Sun, 27 Nov 2005 14:16:42 +0000, Thomas Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :

>For me str.length() != 0 (or !str.equals("")) is clearer than
>str.length() > 0.

But you don't want negative length strings, even if there were such a
thing. You want something with characters in it.

!str.equals("") implies you are avoiding some particular value.

It seems in general better to specify what you do want than what you
don't.  Of course which feels natural then flows from the choice you
made in the deep past.  It feels natural because that's your idiomatic
way of doing it.

My choice would originally have been influenced by an understanding of
the implementation.  str.length()>0 will compile better code even on a
stupid compiler/runtime.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 27 Nov 2005 18:40 GMT
> On Sun, 27 Nov 2005 14:16:42 +0000, Thomas Hawtin
> <usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 5 lines]
> But you don't want negative length strings, even if there were such a
> thing. You want something with characters in it.

I want to be quite clear that String.length never returns any special
values...

> !str.equals("") implies you are avoiding some particular value.

...and that I am avoiding one particular value of string.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Mark - 02 Dec 2005 02:38 GMT
well. lee's solution was quite alright.
thank you guys :p


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.