473,799 Members | 3,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confused on error in array.. (Java)

1 New Member
Hi All,
I am getting the follwoing error

Exception in thread "main" java.lang.Array IndexOutOfBound sException: 0
at Chp8Assign1.mai n(Chp8Assign1.j ava: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.parseIn t (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.lengt h; i++ )
{
sum = sum + myArray [i]; if ( myArray [ i ] > largest )
largest = myArray [ i ]; if ( myArray [ i ] < smallest ) smallest = myArray [ i ];
}
System.out.prin tln("The sum is " + sum);
System.out.prin tln("The average is " + sum/myArray.length) ;
System.out.prin tln("The largest number is " + largest);
System.out.prin tln("The smallest number is " + smallest);
System.out.prin tln("The number of students with scores of 90-100 (A) is " + gradeA);
System.out.prin tln("The number of students with scores of 80-89 (B) is " + gradeB);
System.out.prin tln("The number of students with scores of 70-79 (C) is " + gradeC);
System.out.prin tln("The number of students with scores of 60-69 (D) is " + gradeD);
System.out.prin tln("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 2013
JosAH
11,448 Recognized Expert MVP
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 Contributor
Hi All,
I am getting the follwoing error

Exception in thread "main" java.lang.Array IndexOutOfBound sException: 0
at Chp8Assign1.mai n(Chp8Assign1.j ava: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.parseIn t (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.lengt h; i++ )
{
sum = sum + myArray [i]; if ( myArray [ i ] > largest )
largest = myArray [ i ]; if ( myArray [ i ] < smallest ) smallest = myArray [ i ];
}
System.out.prin tln("The sum is " + sum);
System.out.prin tln("The average is " + sum/myArray.length) ;
System.out.prin tln("The largest number is " + largest);
System.out.prin tln("The smallest number is " + smallest);
System.out.prin tln("The number of students with scores of 90-100 (A) is " + gradeA);
System.out.prin tln("The number of students with scores of 80-89 (B) is " + gradeB);
System.out.prin tln("The number of students with scores of 70-79 (C) is " + gradeC);
System.out.prin tln("The number of students with scores of 60-69 (D) is " + gradeD);
System.out.prin tln("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 ArrayIndexOutOf BoundsException ....
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
4159
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 stopped, and I had no idea what happens. another error is on the last function. I have already declared plane as M x N array, but it keeps giving error like hwone.cpp(50) : warning C4101: 'plane' : unreferenced local variable hwone.cpp(212) :...
1
4170
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 Service returns me an object which is a wrapper around an array of objects. (Earlier I was trying to read the array directly but could not succeed - so I wrapped that array with another object.) Now the primary issue is regarding the chunky characters...
26
3252
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 just came across a funny example in which the opposite is the case. The following is a binary search algorithm in Java. It searches a value in an ordered array a of ints: public static int binarySearch(int a, int key) {
2
6343
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 Collection(); CODE import java.util.*; class CollectionsExample1
3
3591
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 use the previos two. I've gotten all of this code oto compile but I'm still doing something wrong. I'm about to pull my hair out! When I run my CDInventoryDisplay program it comes back with this error: C:\Documents and Settings\Sam>java...
6
5644
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 arrayDescriptor = new ArrayDescriptor(Name of type,cstmt.getConnection()); ARRAY acctList = new ARRAY(arrayDescriptor,conn, flrList.toArray()); cstmt.setInt(1,10); cstmt.setArray(2, acctList);
11
2443
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 the point is at the end of the program. //week 6 Inventory3 // by Tina Jones // IT 215 // october 07/2007
4
1625
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 java/lang/IllegalArgumentException: ". I cannot detect the necessary change I have to make to run my code succesfully.Please anyone can help me to solve this code /code/ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import...
0
3491
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 datasource import java.awt.BorderLayout; import java.awt.Component; import java.io.*; import java.net.URL; import javax.media.*;
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10482
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.