I have written this code in Java on my 64 bit Linux system:
class WrapperClass1
{
public static void main(String s [])
{
int noinput=s.length;
System.out.println("Number of values entered is :- " + noinput);
System.out.println( s[0] + "," + s[1] );
int x = Integer.parseInt(s[0]);
int y = Integer.parseInt(s[1]);
int z = x + y;
System.out.println(" Sum = " + z);
}
};
Now, when I try to compile it, it successfully compiles, but when I execute the program it shows me an exception called:
Number of values entered is :- 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at WrapperClass1.main(WrapperClass1.java:7)
Input : String[]
to which method ? –>
main(String[] args)when to give ? –> While we say
java <className> <input1> <input2> ...to executewhen it throws
ArrayIndexOutOfBoundsException? –>Initial : String s[]
But before you initialise the variable(array), trying to access the index which is not available…
Solution : provide the inputs through the command(as these are command line args)
How to avoid error even if input is not given ? –> check the size of array,
Hope this helps