How to handle the null values of a vector in the conversion from String to Double?

-1

I need to display the top 10 contract rescission clauses, which are in a vector, extracted from a CSV file. Here, when I refer to contract rescission clauses, I mean monetary values (money), more precisely for fine of termination of contract.

What did I think to do?

  • Convert the column with the values of the String clauses to the type Double , because there are very high values;
  • Sort from highest to lowest;
  • Convert back to String; and
  • Add to a List<String> , as this is what I need to return in the method I'm using.
  • The problem happens when I try to convert from String to Double , because in this column there are several people without a defined termination clause value , that is, their values are blank, which I understand to be null.

    If primitive type double is used, it returns an exception saying that it is not possible to convert a String to double . If you use Double , the exception says that the String is empty.

    What can I do to get around this problem?

    Excerpts of the codes I'm using:

    To read the CSV file:

    static String[] jogadores;
    static Double[] rescisao = new Double[17995];
    
    public static BufferedReader lendoCSV(String arquivo, String separador) {
            BufferedReader conteudoArquivo = null;
            String linha = "";
            boolean cabecalhoLido = false;
            int i = 0;
    
            try{
                conteudoArquivo = new BufferedReader(new FileReader(arquivo));
                while((linha = conteudoArquivo.readLine()) != null) {
                    if(!cabecalhoLido) {
                        cabecalhoLido = true;
                        continue;
                    } i++;
    
                    jogadores = linha.split(separador);
                    idade[i] = Integer.parseInt(jogadores[6]);
                    nomeCompleto[i] = jogadores[2];
                    nacionalidade[i] = jogadores[14];
                    nomeDosClubes[i] = jogadores[3];
                    rescisao[i] = Double.parseDouble(jogadores[18]);
                } 
                System.out.println("A leitura do arquivo deu certo!");
            } catch(FileNotFoundException e) {
                System.out.println("Arquivo não encontrado: \n" + e.getMessage());
            } catch(ArrayIndexOutOfBoundsException e ) {
                System.out.println("Indice fora dos limites: \n" + e.getMessage());
            } catch(IOException e) {
                System.out.println("Erro de entrada de dados: \n" + e.getMessage());
            } finally {
                if(conteudoArquivo != null) {
                    try {
                        conteudoArquivo.close();
                    } catch(IOException e) {
                        System.out.println("Erro de entrada de dados: \n" + e.getMessage());
                    }
                }
            }
            return conteudoArquivo;
        }
    

    When I try to convert to Double , because I tried to use double primitive, I get this exception:

      

    Exception in thread "main" java.lang.Error: Unresolved compilation   problem: Type mismatch: can not convert from double to String

    If I try to use Double :

    static Double[] rescisao = new Double[17995];
    

    I get this exception:

      

    Exception in thread "main" java.lang.NumberFormatException: empty   String at sun.misc.FloatingDecimal.readJavaFormatString (Unknown   Source) at sun.misc.FloatingDecimal.parseDouble (Unknown Source) at   java.lang.Double.parseDouble (Unknown Source) at   challenge.Main.lendoCSV (Main.java:46) at   challenge.Main.main (Main.java:171)

    Method that I'm using and that needs to return a List :

    public static List<String> maioresClausulasDeRescisao() {
        List<String> clausulas = new ArrayList<String>();
        for(int i = 0; i < 11; i++) {
            clausulas.add(rescisao[i]);
            System.out.println(clausulas);
        }
        return clausulas;
    }
    
        
    asked by anonymous 09.09.2018 / 06:12

    1 answer

    0

    The problem must occur because you are trying to build a Double object from an empty String. What does throw java.lang.NumberFormatException: empty String, if it was a null String would also occur error, but would be a NullPointerException. You can not pass to the parseDouble method (String s), or to the Double class constructor an empty or null String. One solution is to test the value before constructing the object. You can use try / catch and if the variable of the array where you are reading the values is null or empty, you define a default value to build your Double object and proceed with the program without errors. The following is an example.

        String[] valores = new String[10];
        valores[1] = ""; // teste com null tbm
        Double rescisao;
        try {
            rescisao = Double.valueOf(valores[1]);
        } catch (NumberFormatException | NullPointerException e) {
            e.printStackTrace();
            rescisao = 0.0;
        }
    
        System.out.println("Rescisao: " + rescisao);
    

    Note: You can replace Double with BigDecimal.

        
    09.09.2018 / 22:06