I am getting array bound error but to my mind, array starts from 0, so what is wrong with this code?
public class Quadratic {
public static void main(String
ArrayIndexOutOfBounds exception occur when you are trying to extract the variable at a wrong position in the array.
When you run a program in java after compilation e.x.javac hello
and then executing the class file by java hello
, the 0th position of the args[0]
will be the filename i.e. hello
in this case.
For you to successfully extract argument issued at the run time you have to use n+1 position in the array. For example. If I am running my program as java hello argument
I'll be using args[1]
if I wish to use the issued variable argument
at run time and so on for extracting the associated text.
If you are unsure whether your statement will generate an exception or not, it is always safe to use try & catch
block .Read - Exception Handling