简体   繁体   中英

How to convert string to integer?

So I'm having a hard time with my situation and need some advice. I'm trying to convert my two Strings that I have into integers, so that I can use them in math equations. Here is what I tried, however it brings me an error in the app.

'     equals.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub

                            num1 = edit.getText().toString();
            num2 = edit.getText().toString();

            int first = Integer.parseInt(num1);
            int second = Integer.parseInt(num2);

            edit.setText(first + second);

        }
    });

Is there something that I am doing wrong? Thank you for any help.

EDIT: Yes this is Java. num1 and num2 are strings that I have previously named. What do you mean by trim?

replace your code like this

  num1 = edit.getText().toString(); num2 = edit.getText().toString(); num1=num1.trim(); num2=num2.trim(); int first = Integer.parseInt(num1); int second = Integer.parseInt(num2); 

if any trailing spaces in your string variable , that will cause number format exception while casting it into int or float....

What you are doing wrong is give an Integer value to .setText() function. Everything is correct except that. Try this -

edit.setText(""+(first+second));

EDIT :

The above answer works if and only if an Numeric Value is entered in the EditText.Otherwise it wont be parsed as an integer and you will get an error.

Now,you can also restrict the EditText so that it can only take number as input.

For this,you can do either of the following -

1.In the main XML file,you can include this as an property of EditText -

<EditText android:inputType="number".....>

This will pop up a numeric keypad instead of the qwerty one.

2.If you want to do this programmatically in Java File,

EditText et = new EditText(this); 
et.setInputType(InputType.TYPE_CLASS_NUMBER); 

Make sure you validate the text entered by the user. I mean you have to validate that the text entered is actually an integer.

//trim function will remove leading or trailing blank spaces.
num1 = edit.getText().toString().trim();
num2 = edit.getText().toString().trim();

use Sysout to print num1 and check whether it is actually a number.

Not quite sure but here is my attempt:

equals.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = edit.getText().toString().trim();
            num2 = edit.getText().toString().trim(); // same as num1 why???

            int first = Integer.parseInt(num1);
            int second = Integer.parseInt(num2); // same as first why???

            // convert to text after add them together
            edit.setText("" + (first + second));

        }
    });

将结果从整数转换为字符串

edit.setText(Integer.toString(first + second));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM