Results 1 to 4 of 4
- 10-19-2011, 08:24 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27
To start out with, I am new to posting to forums, so please forgive me if I did not tag my code properly or anything else. The code below is intended to take a comma delimited string, input the string into an array, and then take the individual times, break apart the hours and minutes and feed those into a rounding method (roundTimes) to get the rounded decimal version. I would like ask a couple questions on the "for" loop just below "public static void timeCardShifts(String rec)".
My questions:
1) The code below executes fine when the loop control variable goes no higher than 25, i.e. i<25. The second I go above to reach all the values in the strings i.e. 28, it kicks back the error message, "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27" goes a couple more lines and crashes. In addition, when I run this, the error jumps around happening at different times. I am not sure if I am choking it with my noob code. I will put the results below the code.
2) Can I get some input on how to improve my code?
run:Java Code:public static void testTimeCardShifts() { //Test with the following three input strings: timeCardShifts("10100,7:53,16:35,7:58,16:25,8:01,15:48,8:00,16:01,9:12,14:27,,,,,8:07,13:59,7:51,14:55,,,8:03,14:42,8:02,15:22,10:01,15:58,,"); timeCardShifts("10105,22:55,7:02,22:55,6:58,23:01,7:03,22:49,6:54,23:10,7:13,,,,,22:59,6:04,23:05,7:01,22:59,7:00,23:00,6:52,22:50,7:06,,,,"); timeCardShifts("10175,,,,,,,,,15:00,3:02,14:58,2:45,14:53,3:04,,,,,,,,,14:49,3:02,15:02,2:22,15:01,2:46"); } //end timeCardShifts() public static void testTimeCardShifts(String rec) { String[] splitFields = new String[29]; //empNum and 28 clockin/clockout pairs // rec = rec + "0.0"; //QUIRK splitFields = rec.split(","); //splitFields[1]... [28] for(int i=1; i<29; i++) { String c = splitFields[i]; if(c.equals("")) { splitFields[i] = splitFields[i] + "0:0"; }//end if String d = splitFields[i]; String[] splitTime = {d}; splitTime = d.split(":"); int a = Integer.parseInt(splitTime[0]); int b = Integer.parseInt(splitTime[1]); roundTimes(a,b); System.out.println(roundTimes(a,b)); }//end for public static double roundTimes(int hours, int minutes) { double result = 0; // If statements used to establish quarter hour times if(minutes <= 7) { result=0.00; } else if (minutes >= 8 && minutes <= 22) { result=0.25; } else if(minutes >= 23 && minutes <= 37) { result=0.50; } else if(minutes >= 38 && minutes <= 52) { result=0.75; } else if(minutes >= 53 && minutes <= 59) { result=1.00; } result += hours; return (double) result; }//end roundTimes()
8.0
16.5
8.0
16.5
8.0
15.75
8.0
16.0
9.25
14.5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27
0.0
0.0
0.0
0.0
8.0
14.0
7.75
15.0
0.0
0.0
8.0
14.75
8.0
15.25
10.0
16.0
at AppTimecard.AppTimecard.timeCardShifts(AppTimecard .java:283)
at AppTimecard.AppTimecard.testTimeCardShifts(AppTime card.java:248)
at AppTimecard.AppTimecard.main(AppTimecard.java:240)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)Last edited by junkDNA; 10-19-2011 at 08:34 AM.
- 10-19-2011, 08:48 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27
Your comment is incorrect; Java arrays start at zero, not one. So it would actually be splitFields[0]... [27]Java Code:splitFields = rec.split(","); //splitFields[1]... [28]
In the following for loop, rather than hardcoding the array length into the program, it would be better to use the .length field of the array, like so:
Note that, splitFields.length indicates the number of elements in the array, NOT the index of the last element. So, for example, if splitFields.length had a value of 100, that would mean that the array splitFields has 100 elements, numbered 0...99.Java Code:for(int i=0; i<splitFields.length; i++)
Also, it is unnecessary to initialize splitFields in line 12, because, when you call rec.split(","), the split method actually returns a brand new array which completely replaces the existing array, it does not fill the existing array.
- 10-20-2011, 04:17 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27
Thanks Diamond for the reply. Those are some good suggestions. I will give them a try. Are you able to offer a suggestion for why I am getting that error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27" that jumps around?
- 10-20-2011, 04:58 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27
I'm guessing the exception is a result of your incorrect thinking of how array indices work (the whole starting at 0 vs. 1 thing). As for why it's jumping around, I think that's because, in most Java VMs, System.out and System.err are poorly synchronized, meaning that, if System.out and System.err are printing to the same console, then things printed to them may appear in the wrong order. My guess would be that, in your program itself, the exception is always being thrown at the same spot, put it's just being reported in a different spot due to the lack of synchronization in the VM.
Similar Threads
-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
By dwill19686 in forum New To JavaReplies: 3Last Post: 02-04-2011, 08:31 PM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at wee
By Azaz in forum New To JavaReplies: 4Last Post: 02-02-2011, 04:32 AM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 102
By dark_metal in forum New To JavaReplies: 5Last Post: 04-05-2010, 02:28 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks