Jump to content

Erro comparação strings


ole1990
 Share

Recommended Posts

Boa tarde,

Ao lançar a aplicação quando meto um "-" ou "." ou ".-" o meu programa continua a me dar erro mesmo tendo o IF a verificar

public void onclick(View v){
		String valor_A= numa_view.getText().toString(), valor_B = numb_view.getText().toString();
		BigDecimal valor_a_big = new BigDecimal(valor_A);
		BigDecimal valor_b_big = new BigDecimal(valor_B);
		BigDecimal convert_a = null;
		BigDecimal convert_b = null;
		BigDecimal resultado = null;
		BigDecimal resultadoView = null;

		String test_virgual = numa_view.getText().toString();
   		String test_virguala = numb_view.getText().toString();
   		if(test_virgual == "." || test_virgual == "-." || test_virgual == "-" || test_virguala == "." || test_virguala == "-." || test_virguala == "-"){
   			String message = getString(R.string.impossible);
   			Toast.makeText(distancia_calculadora.this, message, Toast.LENGTH_SHORT).show();
   		}
   		if(temp1_select == 0 || temp2_select == 0 || temp3_select == 0){
   			String message = getString(R.string.impossible);
   			Toast.makeText(distancia_calculadora.this, message, Toast.LENGTH_SHORT).show();
   		}
   		else{

		//convertir numero 2 en meme base que numero 1
		BigDecimal conversao = (BigDecimal.valueOf(0));
		BigDecimal val_a = (valor_select_a);
		BigDecimal val_b = (valor_select_b);
		BigDecimal val_c = (valor_select_c);
		conversao = val_a.divide(val_b);

		//converter numero 2 para mesma base do que numero 1
		BigDecimal num_b_igua_a;
		num_b_igua_a = valor_b_big.divide(conversao);
		BigDecimal a;
		a = num_b_igua_a;


		//efectuar a divisão
		BigDecimal final_divisao;
		final_divisao = valor_a_big.divide(a);
		String ver = String.valueOf(final_divisao);
		double tes = Double.valueOf(ver);

		//efectua conversão final
		BigDecimal convert_txt_view = (BigDecimal.valueOf(0));
		convert_txt_view = val_a.divide(val_c);

		//fazer respectiva conversão e apresentar na txtview
		BigDecimal final_divisao_txtview;
		final_divisao_txtview = final_divisao.multiply(convert_txt_view);
		String total_string =String.valueOf(final_divisao_txtview);
		double total_double = Double.valueOf(total_string);
		result_view.setText(String.valueOf(total_double));
	}
	}
   };
Link to comment
Share on other sites

Eu não sei se te posso ajudar, mas experimenta pôr virgula entre a comparação de valores.

(test_virgual == "." || test_virgual == "-." || test_virgual == "-" || test_virguala == "." || test_virguala == "-." || test_virguala == "-")

( (test_virgual == ".") || (test_virgual == "-.") || (test_virgual == "-") || (test_virguala == ".") || (test_virguala == "-.") || (test_virguala == "-") )

Link to comment
Share on other sites

Knitter tens razão, alterei mas mesmo assim continua igual 😕

private onclickListener divisao_tmp = new onclickListener(){

	@Override
	public void onclick(View v){
		String valor_A= numa_view.getText().toString(), valor_B = numb_view.getText().toString();
		BigDecimal valor_a_big = new BigDecimal(valor_A);
		BigDecimal valor_b_big = new BigDecimal(valor_B);
		BigDecimal convert_a = null;
		BigDecimal convert_b = null;
		BigDecimal resultado = null;
		BigDecimal resultadoView = null;

		String test_virgual = numa_view.getText().toString();
		String test_virguala = numb_view.getText().toString();
		if(valor_A.compareTo(".") == 0 || valor_A.compareTo("-") == 0 || valor_A.compareTo("-.") == 0 || valor_B.compareTo(".") == 0 || valor_B.compareTo("-") == 0 || valor_B.compareTo(".-") == 0){
			String message = getString(R.string.impossible);
			Toast.makeText(distancia_calculadora.this, message+ " 1", Toast.LENGTH_SHORT).show();
		}
		else if(temp1_select == 0 || temp2_select == 0){
			String message = getString(R.string.impossible);
			Toast.makeText(distancia_calculadora.this, message + "2", Toast.LENGTH_SHORT).show();
		}
		else if(test_virguala == "0"){
			String message = getString(R.string.impossible);
			Toast.makeText(distancia_calculadora.this, message+ "3", Toast.LENGTH_SHORT).show();
		}
		else{
			Toast.makeText(distancia_calculadora.this, String.valueOf(valor_A), Toast.LENGTH_SHORT).show();

		//convertir numero 2 en meme base que numero 1
		BigDecimal conversao = (BigDecimal.valueOf(0));
		BigDecimal val_a = (valor_select_a);
		BigDecimal val_b = (valor_select_b);
		BigDecimal val_c = (valor_select_c);
		conversao = val_a.divide(val_b);

		//converter numero 2 para mesma base do que numero 1
		BigDecimal num_b_igua_a;


		num_b_igua_a = valor_b_big.divide(conversao);
		Toast.makeText(distancia_calculadora.this, String.valueOf(valor_b_big), Toast.LENGTH_SHORT).show();

		BigDecimal a;
		a = num_b_igua_a;


		//efectuar a divisão
		BigDecimal final_divisao;
		final_divisao = valor_a_big.divide(a);
		String ver = String.valueOf(final_divisao);
		double tes = Double.valueOf(ver);

		//efectua conversão final
		BigDecimal convert_txt_view = (BigDecimal.valueOf(0));
		convert_txt_view = val_a.divide(val_c);

		//fazer respectiva conversão e apresentar na txtview
		BigDecimal final_divisao_txtview;
		final_divisao_txtview = final_divisao.multiply(convert_txt_view);
		String total_string =String.valueOf(final_divisao_txtview);
		double total_double = Double.valueOf(total_string);
		result_view.setText(String.valueOf(total_double));

	}
	}
};
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.