Java - parseint()

Asked

Viewed 161 times

0

public void OK(View v)
{
    EditText editTextView = (EditText) findViewById(R.id.editTextView);
    String myEditValue = editTextView.getText().toString();
    int equação = Integer.parseInt(myEditValue);
    editTextView.setText(equação + "CERTO!");} 

Gives error when there are characters of type "(" or "+" etc. There is some way to recognize them?

  • You’re saying it’s wrong inside the setText?

  • That is the mistake ; ai after you close the parentheses, you don’t need it

  • It has to stay that way editTextView.setText(equação + "CERTO!")

  • Friend the error occurs because if you have a ( or else + or any other character you will not recognize as an integer in the conversion

  • Anderson Henrique , there is then some way to recognize this " (6*2)+7+5 " all as an int ?

  • No. You have to split and parse each installment individually and do the operations between each thing. Or use a library for that purpose

  • You are trying to implement a "String Calculator". This is not directly supported by the language and, as Isac said, you will need to perform the entire String Stop manually and then perform the calculations, taking operator precedence and so on. You can search for something ready or search tutorials on the Internet, since this is a common 'exercise'.

  • I’ll try to find out if I can get the effect I want. I initially thought it was quite simple, it was enough to stop the whole string, but then I came across this error. In the rest of my code there is a series of equations that use these characters and I had no problem, this because I have always worked with int’s. So either I find another method to get the equations or I have to do as you said. I’ll try to see Thanks for the help

Show 3 more comments

2 answers

2


Find this on the forum, and I found it interesting! I don’t know if it’ll work, but it won’t hurt you to try it there!

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;    

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String infix = "3+2*(4+5)";
System.out.println(engine.eval(infix));

I hope it helps! D

Solution link

  • 1

    @Eduardo Brito, just be careful where you use this, because it is extremely insecure. It can also perform any function that Java can, such as reading and writing to files on the machine.

  • I just can’t care... but I think that would really be the solution

0

There are three very simple ways to solve:

1) Add the attribute below in your Edittext and avoid unwanted characters:

android:inputType="number"

2) Use the methods of the String class itself:

EditText editTextView = (EditText) findViewById(R.id.editTextView);
String myEditValue = editTextView.getText().toString();

//Removendo caracteres indesejados
myEditValue.replace("(", "");
myEditValue.replace("+", "");

int equação = Integer.parseInt(myEditValue);
editTextView.setText(equação + "CERTO!");

3) Search a little about Regex, which is a String used to validate other Strings and if the user enters an unwanted character, show him a message. There is the method of the String class mEditValue.replaceAll(String regex, String substituta), that with Regex prevents you from using step 2 for all cases.

Good Luck.

  • this won’t work simply :/ but thanks for the help anyway

Browser other questions tagged

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