Programme not running - Programmers Heaven

Howdy, Stranger!

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

Categories

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.