I am trying to read file with BufferedReader and at the time of spliting each line of file I want to convert string data at 8th position to be converted to float.(count starts from 0 data)
below is my code :
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TestFloat {
static BufferedReader bin;
String line;
void sumAmount() throws IOException //Perform calculation
{
bin = new BufferedReader(new FileReader("D:\\Extras\\file.txt"));
//String firstline = bin.readLine();
while ((line = bin.readLine()) != null)
{
String data[] = line.split(",");
//System.out.println(data[8]);
System.out.println(Float.valueOf(data[8]));
//System.out.println(java.lang.Float.parseFloat(data[8]))
}
}
public static void main(String[] args)
{
TestFloat ts = new TestFloat();
try {
ts.sumAmount();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
for this code I am getting exception as below :
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at java.lang.Float.valueOf(Float.java:416)
at TestFloat.sumAmount(TestFloat.java:17)
at TestFloat.main(TestFloat.java:24)
one sample line of file.txt is :
20,20160518,262,20160518,00,F&O ABC DEBIT F 160518,000405107289,000405006220,5000000.00,5000000.00,0.00,,
I have tried with parseFloat and valueOf both function but it shows exception. What is the reason behind the fail?