0

I want to store the input from the user in the EditText boxes and store them as a string so I can access it.

I used

               nameIn = name.toString();
               Log.i(null, nameIn); 

(Think thats how you do it and it works fine) but when I use the same code in my int, it won't work.. Now how do I write it so that it can get the users input and store it in my int variable?

This is my code:

        TextView nameText = (TextView) findViewById(R.id.nameText);
    TextView numberText = (TextView) findViewById(R.id.numberText);

    EditText nameInput = (EditText) findViewById(R.id.nameInput);
    EditText numberInput = (EditText) findViewById(R.id.numberInput);

    nameInput.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable name) {
        nameIn = name.toString();
        Log.i(null, nameIn);    
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }



    });


    numberInput.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable number) {

            //this bit im stuck storing the inputted text to an int



        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }});
1
  • Use Integer.parseInt(String) function to convert your string to int Commented Jun 8, 2013 at 6:58

3 Answers 3

3

Use Integer.parseInt(yourString) to get an integer value from a String.

More here.

In your case:

try {
    int myInt = Integer.parseInt(numberInput.getText().toString());
}
catch (NumberFormatException nfe) {
    nfe.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that, but somehow not working.. its giving me an error for getText()
try { int myInt = Integer.parseInt(numberInput.getText().toString()); Log.i("log",Integer.toString(myInt)); } catch (NumberFormatException nfe) { nfe.printStackTrace(); }
What's the error? You're putting this after numberInput has been instantiated right? numberInput is not null?
1

Probably you try to parse non number value. If your input contains spaces pr not a number characters - Integer.parseInt() method will fail and throw NumberFormatException.

To avoid this - add

android:inputType="numberSigned"

attribute to your edittext in layout xml. With this attribute user wouldn't be able to enter any other than positive or negative number.

See details here.

After this you can use Iteger.parseInt() method safely, also I suggest to use String.trim() to remove any whitespace character in the beginning and the end of input if it contains so:

@Override
public void afterTextChanged(Editable number) {
    String numberStr = number.toString().trim();
    //check if your input is not empty
    if (numberStr.isEmpty()) return;
    try {
        //you should create numberIn int type variable like nameIn
        numberIn = Integer.parseInt(numberStr);
    }
    catch (NumberFormatException e) {
        e.printStackTrace();
    }

Comments

0
String nameIn;
int nameInt;
try {
  nameInt = Integer.parseInt(nameIn);
} catch(NumberFormatException e) {
  e.printStackTrace();
  Log.i("Log", "Not a number")
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.