Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
Want to know reason behind NumberFormat Exception in my code

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!