Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,475 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,732 people online right now. Registration is fast and FREE... Join Now!




Array out of bounds Exception

 

Array out of bounds Exception

munish_78

1 Jul, 2009 - 04:35 AM
Post #1

New D.I.C Head
*

Joined: 1 Jul, 2009
Posts: 4

hi, I'm getting the error 'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at SumOfDigits.main(SumOfDigits.java:4)

the code is
public class SumOfDigits{
public static void main(String[] args){
int num = Integer.parseInt(args[0]);
int SOD = 0;
while(num!=0){
SOD=SOD+num%10;
num=num/10;
}
System.out.println("Sum Of Digits="+SOD);
}
}

User is offlineProfile CardPM
+Quote Post


kmangold

RE: Array Out Of Bounds Exception

1 Jul, 2009 - 04:39 AM
Post #2

D.I.C Head
Group Icon

Joined: 24 Jun, 2009
Posts: 166



Thanked: 7 times
Dream Kudos: 75
My Contributions
First of all, enclose code in the proper code tags:

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.

Second, your args array has a length of zero, there are no elements passed to the program when it is run.
User is offlineProfile CardPM
+Quote Post

munish_78

RE: Array Out Of Bounds Exception

1 Jul, 2009 - 04:45 AM
Post #3

New D.I.C Head
*

Joined: 1 Jul, 2009
Posts: 4

QUOTE(kmangold @ 1 Jul, 2009 - 04:39 AM) *

First of all, enclose code in the proper code tags:

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.

Second, your args array has a length of zero, there are no elements passed to the program when it is run.


thx dude for reminiding me dreamincode's policy. secondly, please advise how can i resolve this error and execute the code....thx in advance...
User is offlineProfile CardPM
+Quote Post

kmangold

RE: Array Out Of Bounds Exception

1 Jul, 2009 - 04:49 AM
Post #4

D.I.C Head
Group Icon

Joined: 24 Jun, 2009
Posts: 166



Thanked: 7 times
Dream Kudos: 75
My Contributions
When you run the program, make sure to pass runtime arguments to it.

java SomeClassNameHere 7 8 234 52345234 "hello"

If you are using an IDE, check their site or help section to see how to do this.
User is offlineProfile CardPM
+Quote Post

Gasper

RE: Array Out Of Bounds Exception

1 Jul, 2009 - 04:51 AM
Post #5

D.I.C Head
**

Joined: 29 May, 2009
Posts: 60



Thanked: 11 times
My Contributions
Your code's OK, just run it with argument, like
java SumOfDigits 1234

User is offlineProfile CardPM
+Quote Post

munish_78

RE: Array Out Of Bounds Exception

1 Jul, 2009 - 04:59 AM
Post #6

New D.I.C Head
*

Joined: 1 Jul, 2009
Posts: 4

thx gasper...all ok now..... icon_up.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:23AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month