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 / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

problem...sorry my first time with java

Thread view: 
NewsFree - 19 Mar 2006 22:00 GMT
public class cl_gm {
 public static void main(String[] args) {
   int num = Integer.parseInt(args[0]);
   if(num==1 || num==3 || num==5 || num==7 || num==8 || num==10 || num==12)
     System.out.println(31);
   else if(num==4 || num==6 || num==9 || num==11)
     System.out.println(30);
   else if(num==2)
     System.out.println(28);
   else
     System.out.println("il numero deve essere compreso tra 1 e 12");
 }
}

java.lang.ArrayIndexOutOfBoundsException: 0
at cl_gm.main(cl_gm.java:3)
Exception in thread "main"

Thankyou for any suggestion!
Thomas Hawtin - 19 Mar 2006 22:11 GMT
> java.lang.ArrayIndexOutOfBoundsException: 0
> at cl_gm.main(cl_gm.java:3)
> Exception in thread "main"

You need to supply the month number on the command line, after the class
name:

java cl_gm 2

Tom Hawtin
Signature

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

Rob Skedgell - 19 Mar 2006 22:49 GMT
> public class cl_gm {
>   public static void main(String[] args) {
>     int num = Integer.parseInt(args[0]);
[...snip...]

> java.lang.ArrayIndexOutOfBoundsException: 0
> at cl_gm.main(cl_gm.java:3)
> Exception in thread "main"
>
> Thankyou for any suggestion!

You need to pass the month number as an argument, like this for the
length of March:

java cl_gm 3
31

If you just run

java cl_gm

the String array passed to the main method as args[] will be empty, so
args[0] will be outside the array, and the
ArrayIndexOutOfBoundsException is thrown because of this.

You may wish to familiarise yourself with the javadoc for the exception
<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ArrayIndexOutOfBoundsException.html>

Signature

Rob Skedgell <rob+news@nephelococcygia.demon.co.uk>
All posts made via GoogleGropes killfiled, sorry.
GnuPG/PGP: 7DA3 1579 C0DD 8748 C05A  B984 E2A2 3234 D14B 6DD7

jcsnippets.atspace.com - 19 Mar 2006 23:09 GMT
> public class cl_gm {
>   public static void main(String[] args) {
[quoted text clipped - 15 lines]
>
> Thankyou for any suggestion!

I suspect that, as Thomas already pointed out, you did not supply a value
for args[0]. In other words, you started your program using "java cl_gm"
instead of "java cl_gm 2".

If you start your program like in the first example, the array args will be
null, and there will be no element at index 0, hence the
ArrayIndexOutOfBoundsException.

Posting code of what you are having problems with, is a Good Thing (tm). But
next time you post a question, it might be a good idea to also post a little
description of what you are doing (what is the purpose of your code, what
did you expect it to do, etc...). This clarifies the situation for people
who want to help, and gives them a bigger lead to start from.

Best regards,

JayCee
--
http://jcsnippets.atspace.com/
a collection of source code, tips and tricks
Rob Skedgell - 19 Mar 2006 23:47 GMT
[...]
> If you start your program like in the first example, the array args
> will be null, and there will be no element at index 0, hence the
         ^^^^
> ArrayIndexOutOfBoundsException.

Shouldn't that be "empty", not "null"?

Signature

Rob Skedgell <rob+news@nephelococcygia.demon.co.uk>
All posts made via GoogleGropes killfiled, sorry.
GnuPG/PGP: 7DA3 1579 C0DD 8748 C05A  B984 E2A2 3234 D14B 6DD7

jcsnippets.atspace.com - 22 Mar 2006 00:10 GMT
> [...]
> > If you start your program like in the first example, the array args
[quoted text clipped - 3 lines]
>
> Shouldn't that be "empty", not "null"?

Rob,

Indeed - my mistake. The array args will of course be empty, not null.

Best regards,

JC
--
http://jcsnippets.atspace.com/
a collection of source code, tips and tricks
NewsFree - 20 Mar 2006 00:12 GMT
Thankyou! Thankyou, very helpful people...(I need to improve my Java and my
English too! :)
Roedy Green - 20 Mar 2006 02:19 GMT
>ArrayIndexOutOfBoundsException
for this and other messages, see
http://mindprod.com/jgloss/runerrormessages.html
Signature

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



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.