Results 1 to 17 of 17
Like Tree2Likes
  • 1 Post By deshmukh.niraj04
  • 1 Post By Junky

Thread: java.lang.NumberFormatException: For input string: ""

  1. #1
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default java.lang.NumberFormatException: For input string: ""

    Hi,
    I am trying to convert an int (integer) an empty string.

    I am getting error Msg"

    Java Code:
    java.lang.NumberFormatException: For input string: ""
    How to avoid such type of error.
    trescher likes this.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by deshmukh.niraj04 View Post
    Hi,
    I am trying to convert an int (integer) an empty string.

    I am getting error Msg"

    Java Code:
    java.lang.NumberFormatException: For input string: ""
    How to avoid such type of error.
    Don't try to parse an empty String.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you need more specific help, you'll want to give us a lot more information about your problem including your code, your data, what you're trying to achieve and what's not happening. Please read the link in my signature link on how to ask smart questions to see what kind of information will help us best help you.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Of course you get an error. What integer value is represented by an empty String? If you want to avoid this error then check the String length before parsing it.

  5. #5
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Here's an empty string for you.

    String str = null;
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by stchman View Post
    Here's an empty string for you.

    String str = null;
    Yet again you post incorrect information.

    NULL IS NOT AN EMPTY STRING.

    Null is null and nothing else. An empty string on the other hand is an actual object that contains no characters.

    Java Code:
    String emptyString = "";
    String nullString = null;
    System.out.println(emptyString == nullString);
    System.out.println(emptyString.equals(nullString));
    The output of the above is false false because NULL IS NOT AN EMPTY STRING.
    Fubarable likes this.

  7. #7
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Yet again you post incorrect information.

    NULL IS NOT AN EMPTY STRING.

    Null is null and nothing else. An empty string on the other hand is an actual object that contains no characters.

    Java Code:
    String emptyString = "";
    String nullString = null;
    System.out.println(emptyString == nullString);
    System.out.println(emptyString.equals(nullString));
    The output of the above is false false because NULL IS NOT AN EMPTY STRING.
    Well, for someone that is "ignoring" me you certainly seem to not be.

    As far as your last comment, and empty string and a null string are the same thing.

    Empty string - Wikipedia, the free encyclopedia

    Wait a sec, everyone else is wrong and junky is right.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    This message is hidden because stchman is on your ignore list.
    View Post
    Remove user from ignore list

    This is what I see when logged on. Your posts are viewable when I am not logged on. Besides I have to monitor your posts to rectify all the incorrect information you post.

    Wait a sec, everyone else is wrong and junky is right.
    Of course I am.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    @stchman

    The wikipedia page you linked to is worth reading. "The empty string is distinct from a null reference in that in an object-oriented programming language a null reference to a string type doesn't point to a string object and will cause an error were one to try to perform any operation on it"

    It then goes on to list four characteristic properties of empty strings in many languages:

    (1) It has length zero.
    (2) It is the identity element of the free monoid on the alphabet
    (3) It is its own reverse
    (4) It precedes all other strings

    Post code showing that any of these hold in Java for the string you produced in #4.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Quote Originally Posted by stchman View Post
    As far as your last comment, and empty string and a null string are the same thing.

    Empty string - Wikipedia, the free encyclopedia
    Off the point. You're the only person here talking about a null String. Read the next line in that wikipedia article.

    Quote Originally Posted by Junky View Post
    NULL IS NOT AN EMPTY STRING.
    Correct.
    ------------------------------------------------------------------------------------


    This statement of yours is completely wrong:
    Quote Originally Posted by stchman View Post
    Here's an empty string for you.

    String str = null;
    db
    Last edited by DarrylBurke; 08-19-2011 at 10:51 AM. Reason: for clarity

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by DarrylBurke View Post
    This statement of yours is completely wrong:
    Are you addressing me or stickman?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    The only place I can think of offhand where an empty string is treated as NULL is Oracle, and boy does it cause pain there.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    Are you addressing me or stickman?
    Edited for clarity

    db

  14. #14
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String.

    Hi All,
    This is My code:

    Java Code:
       Workbook workbook = new Workbook();
                workbook.open(filename);
                Worksheet worksheet = workbook.getWorksheets().getSheet(sheetno);
                Cells cells=worksheet.getCells();
                int num=cells.getMaxDataRow();
                int num1=cells.getMaxDataColumn();
                int OCount=1;
                             for (int n1=startpos+1;n1<endpos;n1++)
                 {
                          if (cells.checkCell(n1, Colno).getValue()==null )
                               {         Cell cell=cells.getCell(n1,Colno);
                                        Style style = cells.getCell( n1,Colno).getStyle();
                                        style.setColor(Color.TEAL);
                                        cell.setStyle(style);
                               } else if(cells.checkCell(n1, Colno).getValue().toString().length()==0)
    {
                                      Cell cell=cells.getCell(n1,Colno);
                                        Style style = cells.getCell( n1,Colno).getStyle();
                                        style.setColor(Color.TEAL);
                                        cell.setStyle(style);
      }else{
      double intCounter = Double.parseDouble(cells.checkCell(n1,Colno).getValue().toString());
                                  System.out.println(cells.checkCell(n1,Colno).getValue().toString());
                                  if(intCounter!=Count)
    
                                          {
                                               Cell cell=cells.getCell(n1,Colno);
                                                Style style = cells.getCell( n1,Colno).getStyle();
                                                style.setColor(Color.YELLOW);
                                                cell.setStyle(style);
                                          }
                                      }
                          Count=Count+1;
    
                     }
    workbook.save("C:\\output.xls",FileFormatType.EXCEL97TO2003);

    I am trying to check that Sr no is in sequential order or not. it is working fine if there is no empty string " ". For empty string it throws

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String.

    Thanks in Advance....

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    So, what do your requirements say about invalid (or empty) values in the cells then?

    I don't see that anyone can deal with your "problem" until we know exactly what it is you're supposed to be doing.

  16. #16
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default

    Hi Tolls,

    Thanks for your reply I am checking the sequential order in to my column. if the cell is containing Null value or Empty string it should highlight the cell with Color.TEAL. if number is not in sequence it should highlight the cell in yellow color.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    So what value is it attempting to parse?
    It looks like (from your OP) that it's a space?
    So handle that exception.
    Even better, catch the NumberFormatException and highlight the cell if one is thrown.

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  2. java.lang.NumberFormatException: For input string: ""
    By chathura992 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 01:10 AM
  3. Replies: 5
    Last Post: 04-12-2010, 03:39 PM
  4. jsp insert into database error(java.lang.NumberFormatException: For input string: "")
    By cypher_girl in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-22-2009, 03:14 AM
  5. Replies: 3
    Last Post: 06-07-2009, 12:14 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
  •