Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Programme not running

OmoiiOmoii Posts: 1Member
I wrote this lines of codes to accept values from users with a maximum of 10 numbers using array and then find the average. I also introduced a method for catching exceptions. It compiled once but when I tried running it the second time brought out error messages. However, it is not showing any error message on the Netbeans IDE 6.7 which I use. Please can anyone help.

package assignment;

import java.util.*;
//import java.io.*;
public class CalculateAverage
{


public static void main(String[] args)
{
try
{
int x[] = new int[10];
float sum=0,avg;

Scanner s1 = new Scanner(System.in);
Scanner s2 = new Scanner(System.in);

System.out.println("How many numbers(must be less than 10)");

int numbers = s1.nextInt();

System.out.println("Enter numbers");

int nos = s2.nextInt();

for (int i=0; i<numbers; i++ )
{
System.out.print(x[i]);
sum = sum+x[i];
}
avg = sum/numbers;

System.out.println("Average of the elements=" + avg);
}


catch (Exception e)
{
System.out.println("An error occured; please input numbers less that 10" + e.getMessage());
}

}
}

Comments

  • karan8490karan8490 Posts: 9Member
    import java.io.*;
    class firstarray {

    /**
    * Creates a new instance of firstarray.
    */
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args)
    {
    Console con=System.console();
    int arr[]=new int[10];
    int i,avg,sum;

    System.out.print("enter the values ");

    for(i=0,sum=0;i<arr.length;i++)
    {
    arr[i]=Integer.parseInt(con.readLine());
    sum+=arr[i];
    }
    avg=sum/arr.length;
    System.out.print("avg is "+avg);
    }
    }


    // average is given in round figure.
Sign In or Register to comment.