-1
I need to display the top 10 contract termination clauses, which are in a vector, extracted from a CSV file. Here, when I refer to contract termination clauses, I mean monetary values (money), more precisely for termination fine.
What I thought I’d do?
- Convert the column to the values of the
String
to the guyDouble
, because there are very high values; - Order from the highest to the lowest;
- Convert back to String; and
- Add into a
List<String>
, because that’s what I need to return in the method I’m using.
The problem happens when I try to convert from String
for Double
, for in that column there are several people without a defined termination clause value, that is, its values are blank, which I understand as null.
If primitive type use double
, it returns an exception saying that it is not possible to convert a String
in double
. If use Double
, the exception says that the String is empty.
What can I do to get around this problem?
Snippets 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 take this exception:
Exception in thread "main" java.lang.Error: Unresolved Compilation problem: Type Mismatch: cannot Convert from double to String
If I try to use Double
:
static Double[] rescisao = new Double[17995];
I take 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 I am 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;
}
Before you see this error there is another major error: https://answall.com/q/219211/101.
Double
accept null yes.double
that does not accept. In what was presented there seems to be no problems, the error must be elsewhere.– Maniero
You also need to show the rest of the code associated with conversion.
jogadores
is aString[]
? What value is injogadores[18]
? What kind ofrescisao
?– Isac
Okay, @Maniero and @Isac! Thank you! I will supplement the question! ^_^
– Van Ribeiro
I made corrections to the question. :-)
– Van Ribeiro
To my knowledge double and float were never types to be used in monetary value.
– user28595
@Guess what kind to use, then?
– Van Ribeiro
Bigdecimal is generally used for monetary types.
– user28595
@I’ll try to get.
– Van Ribeiro
@Articuno, I tried, but the problem is still the blank values.
– Van Ribeiro