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?
String
clauses to the
type Double
, because there are very high values; 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;
}