Results 1 to 4 of 4
  1. #1
    lkalaivanan is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default How to remove numberformatexception for string??

    Java Code:
    import java.io.*;
    import java.util.regex.*;
    
    class showfile
    {
      public static void main(String args[]) throws Exception
      {
       int i=1;
       int j=0;
       FileInputStream fin = new FileInputStream("empty");
       DataInputStream in=new DataInputStream(fin);
       BufferedReader br=new BufferedReader(new InputStreamReader(in));
       String strline=null;
       String[] dummy=null;
       String x=null;
       int y=0;
       while ((strline=br.readLine())!= null)
       {
        if(i>1)
        {
         int c=1;
         System.out.println(strline);
         dummy=strline.split(",");
         try{
         while(c<3) 
         {
          x=dummy[c];
          y=Integer.parseInt(x);
          System.out.println(y);
          c++;
          j++;
         }
         }catch(NumberFormatException e){
          System.out.println("Number format exception for input string");
          }
        }
        i++;
       }
       fin.close();
       }
    }
    In above code i am getting error Exception in thread main java.land.numberformatexception for string:"4.6"

    My input is csv file:
    cell_no,cell_x,cell_y,fan-out
    1,4.6,30.23,56
    2,3.2,100.766,67
    3,3.2,98.766,23

    I tried to solve it in different ways but i am not able to remove it,
    Please help me, I am new to java studying myself??
    Last edited by sunde887; 08-10-2011 at 11:21 AM. Reason: Code tags added, [code]...[/code]

  2. #2
    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 using Integer.parseInt(...), and passing in a double, try using Double.parseDouble(...) instead. Perhaps you can test if the current string contains a decimal, if it does, parse a double, otherwise parse an int.

  3. #3
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    Decimal is Double
    Integer is Int

    use Double.parseDouble(....);

  4. #4
    lkalaivanan is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. How to remove comma before an String?
    By bhba73 in forum New To Java
    Replies: 14
    Last Post: 07-28-2011, 04:33 PM
  2. Split string and remove
    By drogba123 in forum New To Java
    Replies: 2
    Last Post: 03-20-2011, 10:52 AM
  3. java.lang.NumberFormatException: For input string: ""
    By chathura992 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 01:10 AM
  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. NumberFormatException: empty String
    By svpriyan in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 10:46 AM

Tags for this Thread

Posting Permissions

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