Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 java.lang.ArrayIndexOutOfBoundsException
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dodobird




PostPosted: Tue Nov 30, 2004 8:02 pm   Post subject: java.lang.ArrayIndexOutOfBoundsException

i dont know what this error is or how to fix it plz help
full error :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at test.main(test.java:6)

----jGRASP wedge2: exit code for process is 1.


this is after i have compiled and try to run the program


heres my program :

import java.io.*;
class test
{
public static void main (String args [])
{
int m = Integer.parseInt(args[0]);
int d = Integer.parseInt(args[1]);
int y = Integer.parseInt(args[2]);
int dy =0;
for (int nm=1; nm<m; nm++)
{
switch (nm)
{
case 4: case 6: case 9: case 11:
dy+=30;
break;
case 2 :
y +=28;
if ((( y % 4) == 0) && ((y % 100)!= 0))
{
dy++;
}
break;
default:
dy +=31;
}
}
dy+=d;
System.out.print(m + "-" + d + "-" + y);
System.out.println(" is day number "
+ dy
+ " in the year");
}
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Nov 30, 2004 8:15 pm   Post subject: (No subject)

code:
int m = Integer.parseInt(args[0]);
int d = Integer.parseInt(args[1]);
int y = Integer.parseInt(args[2]);


You're accessing elements in an array here. Apparently that array doesn't have enough elements.
TheZsterBunny




PostPosted: Tue Nov 30, 2004 8:50 pm   Post subject: (No subject)

wtd wrote:
Apparently that array doesn't have enough elements.


lol.

most of the java error messages are pretty self explanatory if you add spaces just before the capital letters.

'Array Out of Bounds exception.' => You are trying to access part of the array which doesn't exist.

-Z
dodobird




PostPosted: Tue Nov 30, 2004 9:33 pm   Post subject: (No subject)

well im new to java and this program was copyed right out of a learn java now book
wtd




PostPosted: Tue Nov 30, 2004 9:45 pm   Post subject: (No subject)

Well, the array you're tryinbg to access in this program is composed of the arguments you pass to the program at the command-line. How exactly are you running the program?
dodobird




PostPosted: Tue Nov 30, 2004 10:59 pm   Post subject: (No subject)

im sorry if i seem dumb to you but can you please elaborate
zylum




PostPosted: Tue Nov 30, 2004 11:22 pm   Post subject: (No subject)

when you run the program in the command-line, you need to type in some arguments before it starts otherwise the arguments array will be empty and the index you are trying to access will be empty and cause an error... so when you run the program, type in the proper amount of arguments before it starts
dodobird




PostPosted: Tue Nov 30, 2004 11:33 pm   Post subject: (No subject)

so your saying teh program is devoid of any kind of values for teh variables? you know i thought the program was missing that but since i am unfamiliar with java and would assume that the book would not give me an incomplete program
it says
// read in month, day , and year
int m = Integer.parseInt(args[0]);
int d = Integer.parseInt(args[1]);
int y = Integer.parseInt(args[2]);
i was wondering where it was reading from
Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Tue Nov 30, 2004 11:40 pm   Post subject: (No subject)

yeah so when you run your program (from command-line) type in the month day and year and hit enter... it should work fine then
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: