Results 1 to 10 of 10
  1. #1
    whytefish is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default 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:

    Java Code:
    catch(java.lang.NumberFormatException e){
    System.out.println("Input is not a Number, you entered" + (//want the "a" displayed here)
    }
    Thanks!

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    You shown a very short snippet. Can you show a full code?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  4. #4
    whytefish is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    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);
    		}

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  6. #6
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    You can add a
    Java Code:
    private boolean validDouble(String s)
    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.

    I hope that's what you were looking for

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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());

  8. #8
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    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

  9. #9
    whytefish is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks! That helps a lot, i'll look at that API in future.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, please mark your thread solved with the thread tools if you are done.

Similar Threads

  1. Identifying sender on keypress event
    By tomtraxler in forum NetBeans
    Replies: 1
    Last Post: 01-08-2011, 04:49 PM
  2. Help me ! identifying the mistake
    By o0oNorao0o in forum New To Java
    Replies: 6
    Last Post: 11-06-2010, 10:03 PM
  3. Replies: 3
    Last Post: 11-02-2010, 09:10 AM
  4. Most of my trouble is identifying a pattern
    By ElkNinja in forum New To Java
    Replies: 4
    Last Post: 10-10-2010, 06:17 AM
  5. help with identifying components of a gui
    By jaytee in forum New To Java
    Replies: 1
    Last Post: 03-08-2010, 02:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •