Results 1 to 8 of 8
Thread: Exception in thread "main"
- 06-09-2011, 12:27 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Exception in thread "main"
Hi. The rest of my code works fine, but when I use a double my code crashes on line 32. I get a "Exception in thread 'main' java.lang.ArrayIndexOutOfBoundsException: 0
at Time.<init>(Time.java:32)
at Time.main(Time.java:55)" error.
My line 55 just calls up the Time constructor, so I doubt that it's the problem. I don't know why this keeps happening, and I can't fix it. Does anyone have any ideas?
[code]
Time ( double dec )
{
String s = Double.toString( dec );
String[] array = s.split(".");
int numWhole = Integer.parseInt(array[0]); <--- Line 32
int numDec = ((Integer.parseInt(array[1]))/10)*60;
System.out.println( numWhole + ":" + numDec + " Time double constructor" );
}
[\code]
- 06-09-2011, 12:39 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
The array index in question is 0. So if it is out of bounds then array is an empty array (ie does not have an index 0).
You can check what is going on by printing the string s and/or the contents of the array.
Java Code:Time ( double dec ) { String s = Double.toString( dec ); [color=blue]System.out.println("In Time constructor: s is " + s);[/color] String[] array = s.split("."); int numWhole = Integer.parseInt(array[0]); <--- Line 32 int numDec = ((Integer.parseInt(array[1]))/10)*60; System.out.println( numWhole + ":" + numDec + " Time double constructor" ); }
- 06-09-2011, 12:52 AM #3
The problem is that s.split takes a regex as the argument, and "." is a regex wildcard for "any character". Since every part of s is "any character", it won't find anything to put into the array. As such, the array will be empty. Use s.split("\\.") instead.
- 06-09-2011, 02:34 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Agree... I didn't notice that!
I don't know about split("\\.") though. Double.toString() can return things without a dot in them and throwing an exception in this case would be a Good Thing. But it can also return things that will make the subsequent Integer.parseInt(array[1]) throw a NumberFormatException which is Less Good. Better in this case, I think, to avoid the quirkiness of toString() - and the dangers of regex - and fill the array by hand using the % operator. We start with a numerical quantity and we want to end up with an array of numerical quantities: why detour into dealing with Strings?
- 06-09-2011, 02:50 AM #5
Actually, Double.toString(double) will always return a string with a dot in it.
Output: 10.0Java Code:class DoubleTest { public static void main(String[] args) { double d=10; System.out.println(Double.toString(d)); } }
The problem with this, however, is: A string of 5.03 will throw a NumberFormatException: Integer.toInt(String s) isn't too happy about leading zeros. Could of course be dealt with in other ways. As for using %... That would return a double between 0 and 0.99999... which is hard to turn into an integer if you don't know how long it is. In that case, taking the String detour is an easier concept to grasp. However, if we decide that we want a maximum of a certain number of decimals taken into consideration, it becomes much easier, and it becomes far easier (and quicker by an order of fifty!) to use % and /.
- 06-09-2011, 02:51 AM #6
Or if going down the String path do indexOf to get the location of the decimal point and then substring if it is greater than 0.
- 06-09-2011, 03:01 AM #7
Hmm. I might have to retract the comment about Integer.toInt not being happy about leading zeros. I tried it again and it passed without a problem. Odd, because I got a NumberFormatException with such a string when I first tested. Must've been some trailing space or some such that I didn't see.
And yes, using indexOf is about three times faster than using split, so use that if possible.
- 06-09-2011, 03:26 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
I was thinking about "NaN" and "Infinity" which as I suggested may well deserve to throw an exception.Actually, Double.toString(double) will always return a string with a dot in it.
The NFEs will occur when toString() acts on an argument <10E-3 or >10E7.
I'm not completely sure what numDec is supposed to be. If formatting is important, then Strings would be good. But otherwise the OP now has three different approaches (split(), indexOf() and arithmetic operations) and should decide or try all three. And, maybe, let us know...
Similar Threads
-
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 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 -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks