Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda