This discussion is archived
3 Replies Latest reply: Sep 26, 2009 10:57 AM by 3004 RSS

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

843789 Newbie
Currently Being Moderated
import java.util.*;
import java.io.*;
/**
 *  Write a one-sentence summary of your program here.
 *  Follow it with additional details about its purpose, what
 *  it represents, and how to use it.
 *
 *  @author (your name goes here)
 *  @version (place the date here)
 */
public class SumDigits
{
    // Program-wide input and output streams
    private PrintStream cout = System.out;
    private Scanner cin = new Scanner(System.in);
    
    /**
     *  Place a one-sentence description of your run method here.
     *  The run() method is the entry-point for your program.
     * @param args 
     */
    public void run(String[] args)
    {
        System.out.println("Vu, John: Sum Digits");
        System.out.println("=========================");
        System.out.println("Enter a number from 0 to 1000: ");
        int to = cin.nextInt();
        int sum, i,a,d;
        a = Integer.parseInt(args[0]);
        sum = 0;
        for(i=1;i<=10;i++)
        {
        d = a%10;
        a = a/10;
        sum=sum + d; 
        System.out.println("Sum of the digits is: " + sum);
        }
        
    }
    
    /**
     * The standard Java entry point. DON'T MODIFY.
     * @param args the command-line arguments.
     */
    public static void main(String...args)
    {
        SumDigits app = new SumDigits();
        app.run(args);
    }
}
this is my code

when I ran the console, i got this:

Vu, John: Sum Digits
=========================
Enter a number from 0 to 1000:
999
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
     at SumDigits.run(SumDigits.java:29)
     at SumDigits.main(SumDigits.java:48)

can anyone explain to me what is Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException?