Results 1 to 10 of 10
- 03-16-2011, 11:56 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Error Handing, identifying offending string
Was wondering if anyone knows any way to refer to the offending input string when handling a NumberFormatException error. Below is an example of the error i'm handling, I want to be able to include the "a" in the explanation.
Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
Catch statement:
Thanks!Java Code:catch(java.lang.NumberFormatException e){ System.out.println("Input is not a Number, you entered" + (//want the "a" displayed here) }
- 03-16-2011, 11:59 AM #2
You shown a very short snippet. Can you show a full code?
Skype: petrarsentev
http://TrackStudio.com
- 03-16-2011, 12:00 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Is just displaying the full string incorrect? You can change it to something like "All items in the input must be numbers".
You can also probably get the offending item with indexOf, but it will take some thinking on your part.
- 03-16-2011, 12:09 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
I'm just after a way to refer directly to a variable which contains a string rather than a double, and include it in the explanation. Thanks!
Java Code:/** //convert strings to doubles try{ Side1 = Double.parseDouble(Side1str); Side2 = Double.parseDouble(Side2str); Angle = Double.parseDouble(Anglestr); } catch(java.lang.NumberFormatException e){ System.out.println("Input is not a Number") } EculidArea = AreaCalc(); System.out.println(EculidArea); }
- 03-16-2011, 12:18 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Try seeing what e.getMessage() returns. Im not exactly sure of the answer, but a quick search of the core API for number format exception found this method.
- 03-16-2011, 12:19 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
You can add a
method to your class, and keep using it to check for valid double valies. if the method returns false, you can tell the user that the input was'nt valid, along with that input.Java Code:private boolean validDouble(String s)
I hope that's what you were looking for
- 03-16-2011, 12:23 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I just did a quick test of getMessage() and it returns for the input string: "offending string". You can either just print all of that, or just the string or just the incorrect letters.
To get just the string you could do something like
Java Code:StringBuilder errorString = new StringBuilder(e.getMessage()); errorString.delete(0, 18); System.out.println(errorString.toString());
- 03-16-2011, 12:24 PM #8
see
Java Code:package com.trackstudio.tools; public class ParserNumber { /** * Method return case Number or null. it checks follows class Integer, Long, Float, Double * @param value value * @param cl Class for cast case * @param <T> Generic * @return return need value */ public static <T extends Number> Number parseValueOrNull(String value, Class<T> cl) { try { if (value != null) { if (cl.equals(Integer.class)) { return Integer.parseInt(value); } else if (cl.equals(Long.class)) { return Long.parseLong(value); } else if (cl.equals(Float.class)) { return Float.parseFloat(value); } else if (cl.equals(Double.class)) { return Double.parseDouble(value); } } } catch (NumberFormatException e) { if (value != null && !value.isEmpty()) { System.out.println("NumberFormatException value - " + value + " cast case - " + cl.getSimpleName()); } return null; } return null; } }Skype: petrarsentev
http://TrackStudio.com
- 03-16-2011, 12:26 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Thanks! That helps a lot, i'll look at that API in future.
- 03-16-2011, 12:28 PM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Identifying sender on keypress event
By tomtraxler in forum NetBeansReplies: 1Last Post: 01-08-2011, 04:49 PM -
Help me ! identifying the mistake
By o0oNorao0o in forum New To JavaReplies: 6Last Post: 11-06-2010, 10:03 PM -
Could you tell me how to fix the offending 3 lines try block
By achab in forum Advanced JavaReplies: 3Last Post: 11-02-2010, 09:10 AM -
Most of my trouble is identifying a pattern
By ElkNinja in forum New To JavaReplies: 4Last Post: 10-10-2010, 06:17 AM -
help with identifying components of a gui
By jaytee in forum New To JavaReplies: 1Last Post: 03-08-2010, 02:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks