473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Java having problem to find an average

1
When I run the following program to find an average it shows: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at average.Average.main(Average.java:32)"

Can anyone help me plz.




Expand|Select|Wrap|Line Numbers
  1. package average;
  2. import java.util.Scanner;
  3.  
  4.  
  5. /**
  6.  *
  7.  * @author MdTazimulIslam
  8.  */
  9. public class Average {
  10.  
  11.     /**
  12.      * @param args the command line arguments
  13.      */
  14.     public static void main(String[] args) {
  15.         // TODO code application logic here
  16.     double avg;
  17.         int sum=0;
  18.         Scanner input=new Scanner(System.in);
  19.     int i=0;
  20.  
  21.     int n;
  22.     System.out.println("Array length: ");
  23.     n=input.nextInt();
  24.     int []A=new int[i];
  25.     System.out.println("The numbers: ");
  26.     for(i=0;i<n;i++)
  27.     A[i]=input.nextInt();
  28.     for(i=0;i<n;i++)
  29.     {
  30.         sum=sum+A[i];
  31.     }
  32.     avg=sum/n;
  33.             System.out.println("The average is: "+avg);
  34.  
  35.  
  36.  
  37.     }
  38.  
  39. }
Mar 24 '15 #1
1 1361
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code or formatted data.

You're creating the array based on I when the array size is stored in n.
Mar 25 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Day9901 | last post by:
Hi, I need to port a C++/Linux program to Java, but cannot find a good match as I used Linux "Named Pipe". I've been studying much on Linux and decided to Named Pipe for my app's multi-processes...
2
by: Dave Brueck | last post by:
Below is some information I collected from a *small* project in which I wrote a Python version of a Java application. I share this info only as a data point (rather than trying to say this data...
1
by: Meena Lalwani | last post by:
Hi All, I am working on the performance measurement for my dot net application. There are 5000 to 10000 users who logged in with my application. I am able to find out which user logged in ,...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
8
by: jason.m.ho | last post by:
>From the common user perspective (like my grandma), why would they care if its a java applet or an ajax application? Say I want to make a chat system on my website...If i'm doing really involved...
9
by: TomC | last post by:
Is there any resource where someone who is familiar with Java might find which .NET classes are similar to classes they are familiar with in Java? For example, I have a project that I'd like to...
5
by: NoOoR | last post by:
I want to write a program that read n numbers and perform some mathematical tasks on them. The task available for the user is as follow : 1- Finding the average 2- Find the maximum...
0
by: abhilash12 | last post by:
is there any searchengine using with java for find word in ms word file pls help me
2
by: cemma | last post by:
Hello! So i am a new programmer and i really need some help. I am trying to find the Population Standard Deviation of 7 numbers which the user inputs. Please see below my double...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.