Results 1 to 4 of 4
Thread: help with simple java program
- 07-25-2007, 04:34 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 43
- Rep Power
- 0
help with simple java program
I did a simple program but I have a problem
this is the code
Java Code:public class Test { private static int[] numbers=new int[4]; public static void main( String args[] ) { for (int i = 1;i<=4;i++) { numbers[i]=1; } } }
Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Test.main(PayrollProgramExecute.java:16)
- 07-25-2007, 05:54 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 1
- Rep Power
- 0
try either
(int i=0;i<4;i++)//as index starts with 0
or
(int i=0;i<numbers.length;i++)//much better
- 07-25-2007, 10:29 AM #3
Member
- Join Date
- Jul 2007
- Posts
- 8
- Rep Power
- 0
in an array the indexing begins from 0 and not from one..
so the flaw in yr program is that u started with one and went till 4
instead u shld have done
for (int i = 0;i<=3;i++)
private static int[] numbers=new int[4]; means u r creatin an array with four elements with index 0,1,2,3 and not 4.............
i guess you got the point...........
- 07-30-2007, 10:40 AM #4
Member
- Join Date
- Jul 2007
- Posts
- 74
- Rep Power
- 0
Similar Threads
-
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 03:40 PM -
Peculiarty in code of simple program...
By Kreuz14 in forum New To JavaReplies: 4Last Post: 01-23-2008, 04:27 AM -
Simple java
By abhiN in forum New To JavaReplies: 1Last Post: 01-16-2008, 11:13 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 10:33 PM -
help with simple program in java
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 09:03 PM
Bookmarks