Sum in Textview

Asked

Viewed 138 times

2

I have a Json that returns the values of positive and negative, and that sets the text in mine TextView. This is the calculation I try to do, but it returns a invalid double:

public double num1,num2,resultado;

saldo = (TextView) findViewById(R.id.saldouser);
negativo = (TextView) findViewById(R.id.vendaliv);
positivo = (TextView) findViewById(R.id.compraliv);
date = (TextView) findViewById(R.id.datatrans);

num1 = Double.parseDouble(positivo.getText().toString());
num2 = Double.parseDouble(negativo.getText().toString());
resultado = num1-num2;
System.out.println(resultado);
saldo.setText(resultado);

The mistake is:

Caused by: java.lang.Numberformatexception: Invalid double: ""

  • Could you leave the complete error in the question? It is better for those who answer, understand what is happening.

  • I made the edit, I inserted the error ..

1 answer

2


Of the two:

1) You use in the text a value like 12,9 or 10,0with comma, as we use in Brazil. But Double uses . as it is used in the US, so he can not interpret that 12,9 is 12.9 and explode.

2) You try to parse an empty string, which is invalid because it does not automatically convert to 0.

Sugestao, use a Try catch block to treat this Exception and/or a function to convert your comma to point if that’s the problem.

  • It’s probably the same empty string..

  • @Thiago by its error is the empty String, if solved your problem accept the answer ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.