1

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?

GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
Student
  • 41
  • 1
  • 6
  • 1
    Debug before coming here. Print out what that String token holds just before you try to parse it. – Hovercraft Full Of Eels Apr 29 '17 at 15:21
  • Before parsing value to a number format, you should check it for null and also check it's length should be greater than 0. It is possible to have blank value at 8th in some line in your txt file, which has caused the exception. – Nishesh Pratap Singh Apr 29 '17 at 15:24
  • i have already tried printing data before parsing it. It gives same string as I want. its string 5000000.00..which it should be. – Student Apr 29 '17 at 15:27
  • 1
    Not every time Student. You're using a while loop, and at least one time in that loop, it doesn't. You need to debug further. – Hovercraft Full Of Eels Apr 29 '17 at 15:29
  • can you please explain it more?I am not getting you. – Student Apr 29 '17 at 15:30
  • 1
    Again, uncomment the println's and see what they say. Change them so that they always print something: `System.out.println("data[8]: " + data[8]);` and you'll see the last time the while loop loops, you're getting an empty String. Then do further debugging to see why. – Hovercraft Full Of Eels Apr 29 '17 at 15:32
  • 1
    [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/q/25385173/5221149) – Andreas Apr 29 '17 at 15:34
  • Thank you everyone. I got to know Importance of debugging now. – Student Apr 29 '17 at 15:38

1 Answers1

0

java.lang.NumberFormatException: empty String

as the error states you're attempting to parse an empty string.

one solution is to use an if statement to guard off the NumberFormatException(only for empty strings that is, you could still get the NumberFormatException for unparseable strings).

if(data[8] != null && data[8].length() > 0){
     System.out.println(Float.valueOf(data[8]));
     System.out.println(java.lang.Float.parseFloat(data[8]));
}

you'll need to go through the debugger step by step and see what is the real issue behind it.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126