Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I beginner and just started java programming and by doing its practice I got an error in the Example of Area of Triangle which couldn't solved out. I tried everything but I don't get it. Can anybody tell me why this error comes in following program.

C#
/*Program to calculate area of a triangle when three sides are passed as input .*/
class Area
{
    public static void main(String args[])
    {
        int s,sum=0,n;
        double area;
         n=args.length;
        int []a=new int [n];    //Declaration of array to define three sides of a triangle.
        for(int i=0;i<=n;i++)
        {
            if(n<=3)
            {
                a[i]=Integer.parseInt(args[i]);
                sum=sum+a[i];
            }
            else
            {
                System.out.println("You Should Enter Only Three Values");
                break;
            }
        }
        if(n<=3)
        {
            s=sum/2;
            area=Math.sqrt(s*(s-a[1])*(s-a[2])*(s-a[3]));
                        System.out.println("Area = "+area);
        }
        }
}


And after that, I used javac compiler:

>javac area.java
>java area 3 5 6

After That the following error comes.

>" Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Area.main<Area.java:14>

Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 20-Sep-14 12:46pm    
1) Use the debugger;
2) When asking question, comment that line 14 with exception information. Otherwise — how is going to count your lines to find the one where the exception is thrown?
—SA
VISH_a_CODE 20-Sep-14 13:11pm    
Very well thanks. I will do it from next time.
Sergey Alexandrovich Kryukov 20-Sep-14 14:04pm    
Thank you for understanding. If your problem is not yet completely solved, please use the debugger.
I answered on the problem you spotted, but the rest of the code can cause problems.
—SA

In addition to Sergey's answer, this part of code is broken too:
Quote:
if(n<=3)
{
s=sum/2;
area=Math.sqrt(s*(s-a[1])*(s-a[2])*(s-a[3]));
System.out.println("Area = "+area);
}


For instance, if n=2 then the if condition evaluates to true and code block is executed, but both a[2] and a[3] don't exist.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 20-Sep-14 14:03pm    
5.—SA
VISH_a_CODE 20-Sep-14 14:08pm    
Yes I fixed it. Thanks to help.
Sergey Alexandrovich Kryukov 20-Sep-14 14:13pm    
Very good.
Now, will you accept both answers formally (green "Accept" button)?
—SA
For maximum i, args[i] does not exist, because the user did not provide so many arguments.
It happened because of your bug:
Java
for(int i=0;i<=n;i++)

Can be fixed by writing:
Java
for (int i=0; i<n; i++)

—SA
 
Share this answer
 
v2
Comments
VISH_a_CODE 20-Sep-14 13:04pm    
Sir, it Still it did't work. I am trying it too.
Sergey Alexandrovich Kryukov 20-Sep-14 14:02pm    
Then something else does not work. I answered your question related to the line you referred to.
You should not "try", you should know exactly what you are doing.
—SA
CPallini 20-Sep-14 13:41pm    
5.
Sergey Alexandrovich Kryukov 20-Sep-14 14:03pm    
Thank you, Carlo.
—SA
I solved it. Thanks to help.

The Edited Program is,

C#
/*Program to calculate area of a triangle when three sides are passed as input .*/
class Area
{
    public static void main(String args[])
    {
        int s,sum=0,n,ss,m=1;
        double area;
        n=args.length;
        int []a=new int [n];    //Declaration of array to define three sides of a triangle.
        for(int i=0;i<n;i++)
        {
            if(n==3)
            {
                a[i]=Integer.parseInt(args[i]);
            }
            else
            {
                System.out.println("Please Enter Only Three Values");
                break;
            }
            sum=sum+a[i];
        }
        if(n==3)
        {
            s=sum/2;
            for(int i=0;i<n;i++)
            {
            ss=(s-a[i]);           //ss Assigns s-a[1] to s-a[3] separately 
            m=m*ss;                //Passing value of ss to m 
            }
            area=Math.sqrt(s*m);   
                        System.out.println("Area of Triangle = "+area);
        }
        }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Sep-14 14:22pm    
You are welcome.

But this post is not needed or even appropriate. This is not really an answer. You could instead add this code to the question with proper comments, using "Improve question" above.

Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900