-1

I have the same error in every code I run on Eclipse IDE or on Notepad, Also it was not earlier. In this code I also having same error which I mentioned below

Code:

package WorkJava;

public class TypeCast{

        public static void main(String ar[]) {

            int sum=0;

            for(int i=0;i<5;i++) {

            int a=Integer.parseInt(ar[i]);

            sum=sum+a;

            }

            System.out.println("the sum="+sum);
        }

error:-

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at WorkJava.TypeCast.main(TypeCast.java:1
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
S C
  • 11
  • 3
  • 1
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – OH GOD SPIDERS Apr 28 '21 at 14:11
  • 4
    Your program expects you to pass it at least 5 numbers as parameters when you start it. If you don't pass any parameters `String ar[]` will be empty and your loop fails. – OH GOD SPIDERS Apr 28 '21 at 14:12

1 Answers1

1

you are getting the error because you are not passing program arguments so to pass it in eclipse open your class click run on the menu bar the run configurations then argument then in program arguments type 5 integers like the following with spaces.

2 0 4 5 6

Yeshwin Verma
  • 282
  • 3
  • 16