Results 1 to 4 of 4
- 08-10-2011, 11:15 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
How to remove numberformatexception for string??
In above code i am getting error Exception in thread main java.land.numberformatexception for string:"4.6"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(); } }
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]
- 08-10-2011, 11:25 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 08-10-2011, 12:24 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
Decimal is Double
Integer is Int
use Double.parseDouble(....);
- 08-11-2011, 05:38 AM #4
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
How to remove comma before an String?
By bhba73 in forum New To JavaReplies: 14Last Post: 07-28-2011, 04:33 PM -
Split string and remove
By drogba123 in forum New To JavaReplies: 2Last Post: 03-20-2011, 10:52 AM -
java.lang.NumberFormatException: For input string: ""
By chathura992 in forum New To JavaReplies: 3Last Post: 01-08-2011, 01:10 AM -
jsp insert into database error(java.lang.NumberFormatException: For input string: "")
By cypher_girl in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-22-2009, 03:14 AM -
NumberFormatException: empty String
By svpriyan in forum New To JavaReplies: 1Last Post: 08-12-2009, 10:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks