473,396 Members | 1,714 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,396 software developers and data experts.

Confused on error in array.. (Java)

Hi All,
I am getting the follwoing error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Chp8Assign1.main(Chp8Assign1.java:37)
Press any key to continue...

In this program...

public class Chp8Assign1 {
public static void main (String args [ ] )
{
int myArray [ ] = new int [ args.length ]; // for loop to convert String args array to integer myArray
int gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;
for (int a = 0; a < args.length ; a++)
{
myArray [a ] = Integer.parseInt (args [ a ] );
if(myArray [a] >= 90)
{
gradeA++;
}
else if(myArray [a] >= 80 && myArray [a] <= 89)
{
gradeB++;
}
else if(myArray [a] >= 70 && myArray [a] <= 79)
{
gradeC++;
}
else if(myArray [a] >= 60 && myArray [a] <= 69)
{
gradeD++;
}
else if( myArray [a] <= 60)
{
gradeF++;
}
}
int sum = 0;

int largest = myArray[0];
int smallest = myArray[0];
// for loop to find sum, largest and smallest
for (int i = 0; i<myArray.length; i++ )
{
sum = sum + myArray [i]; if ( myArray [ i ] > largest )
largest = myArray [ i ]; if ( myArray [ i ] < smallest ) smallest = myArray [ i ];
}
System.out.println("The sum is " + sum);
System.out.println("The average is " + sum/myArray.length);
System.out.println("The largest number is " + largest);
System.out.println("The smallest number is " + smallest);
System.out.println("The number of students with scores of 90-100 (A) is " + gradeA);
System.out.println("The number of students with scores of 80-89 (B) is " + gradeB);
System.out.println("The number of students with scores of 70-79 (C) is " + gradeC);
System.out.println("The number of students with scores of 60-69 (D) is " + gradeD);
System.out.println("The number of students with scores below 60 (F) is " + gradeF);
} // ends main
}


Could anyone point me in the right direction?

Thanks.
Apr 4 '08 #1
2 1999
JosAH
11,448 Expert 8TB
Could anyone point me in the right direction?

Thanks.
You're defining an array as long as the number of arguments you supplied on
the command line. You didn't supply any arguments so you end up with an
array having 0 (zero) elements.

kind regards,

Jos
Apr 4 '08 #2
sukatoa
539 512MB
Hi All,
I am getting the follwoing error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Chp8Assign1.main(Chp8Assign1.java:37)
Press any key to continue...

In this program...

public class Chp8Assign1 {
public static void main (String args [ ] )
{
int myArray [ ] = new int [ args.length ]; // for loop to convert String args array to integer myArray
int gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;
for (int a = 0; a < args.length ; a++)
{
myArray [a ] = Integer.parseInt (args [ a ] );
if(myArray [a] >= 90)
{
gradeA++;
}
else if(myArray [a] >= 80 && myArray [a] <= 89)
{
gradeB++;
}
else if(myArray [a] >= 70 && myArray [a] <= 79)
{
gradeC++;
}
else if(myArray [a] >= 60 && myArray [a] <= 69)
{
gradeD++;
}
else if( myArray [a] <= 60)
{
gradeF++;
}
}
int sum = 0;

int largest = myArray[0];
int smallest = myArray[0];
// for loop to find sum, largest and smallest
for (int i = 0; i<myArray.length; i++ )
{
sum = sum + myArray [i]; if ( myArray [ i ] > largest )
largest = myArray [ i ]; if ( myArray [ i ] < smallest ) smallest = myArray [ i ];
}
System.out.println("The sum is " + sum);
System.out.println("The average is " + sum/myArray.length);
System.out.println("The largest number is " + largest);
System.out.println("The smallest number is " + smallest);
System.out.println("The number of students with scores of 90-100 (A) is " + gradeA);
System.out.println("The number of students with scores of 80-89 (B) is " + gradeB);
System.out.println("The number of students with scores of 70-79 (C) is " + gradeC);
System.out.println("The number of students with scores of 60-69 (D) is " + gradeD);
System.out.println("The number of students with scores below 60 (F) is " + gradeF);
} // ends main
}


Could anyone point me in the right direction?

Thanks.
There is nothing wrong with your code....

except that you tend to execute it without initial values at execution....

Expand|Select|Wrap|Line Numbers
  1. java Chp8Assign1
That may lead to ArrayIndexOutOfBoundsException....
Some codes are depending on args values... If none, you have to put a trap on your code.... or you could use try/catch block....

Expand|Select|Wrap|Line Numbers
  1. java Chp8Assign1 98 89 95 97 92
regards,
sukatoa
Apr 4 '08 #3

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

Similar topics

3
by: Victor | last post by:
I'm trying to run this java program, but somehow the program always quit w/o giving any error msg at all. it happenned inside the first case statements. Strangely, after printing happen2, it just...
1
by: Vaibhav Modak | last post by:
Hi All, I have a Web Service written in Java (Web Logic) and I am trying to call it in my ASP. NET client. I am facing a problem while getting the data from the Web Service Method. My Web...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
2
by: champion | last post by:
I am the following when i try to run my java program. I have included the code below CollectionExample1.java java.util.Collection is abstract; cannot be instatiated Collection col=new...
3
sammyboy78
by: sammyboy78 | last post by:
Hello again, This time I'm creating a program that creates an array of CD objects and then displays the information. I've created a CD class, a CDInventory class and then a CDInventoryDisplay to...
6
by: meru | last post by:
Hi The highlighted below is the sample code I used for calling my stored procedure. Callable statement cstmt = conn.prepareCall( BEGIN STOREDPROCNAME(?,?,?);END;); ArrayDescriptor...
11
by: TinaJones095 | last post by:
Hello I am going to give a program that I have done, but I have to modifiy it, but I need help okay can you help ? Here the program I need help to straighten up below: the Java error is right at...
4
by: surja | last post by:
Hi, I have written a code to download images from a server end desktop, but while running the code ,WTK is showing a runtime error " Create image from Byte array Uncaught exception...
0
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.