0

I have an EditText that only accepts numerical input and I am using the following code to turn that input to a string so I can use it later.

scoreString = Integer.parseInt(teamScore.getText().toString());

The problem is... when I use a setText(), it only says "0" in:

previewText.setText( scoreString + " :");

Why won't the integer say what the user put in the edittext?

EDIT: If I'm using a setText with a ton of strings (6-8 strings in it), will this disrupt the ability to use the number in there?

-I even collected the final chunk of strings and made them into one whole string, THEN used the setText as the finalOutput and it didn't work (see below)

finalOutput = (sportName + ": " + team1NameString
                    + " " + team1ScoreString + ", " + team2NameString + " "
                    + team2ScoreString + " - " + quarterString + " "
                    + descriptionString);
            generatePreview.setText(finalOutput + "");

(sorry I keep changing variable names, just pay attention to the format)

1
  • You might need to trim that String first but I don't remember 100% if that's really necessary: Integer.parseInt(teamScore.getText().toString().trim()) Commented Feb 17, 2013 at 5:25

1 Answer 1

1

Hmm, I can confirm the following works the way it sounded like you wanted it to (meaning if I enter "65" in the EditView, it outputs "65 :" in the TextView):

    EditText mEditView = (EditText)findViewById(R.id.myedittext);
    TextView mTextView = (TextView)findViewById(R.id.mytextview);

    int scoreString = Integer.parseInt(mEditView.getText().toString());
    mTextView.setText(scoreString + " :");

Is your setup the same?

Sign up to request clarification or add additional context in comments.

3 Comments

The number of Strings shouldn't matter. I manually set the variables you mentioned/copied your finalOutput string in my code and it still worked as expected (it doesn't fit in a comment, but String team1Name="Team 1", String sportName="Sport", etc.). For debugging purposes, is it all variables or just certain ones that are the problem? Do your variable types/names match up everywhere? Did you make sure to do the findViewByid after doing the right setContentView? It's probably something that's easy to overlook.
I've double checked everything and I still can't figure out the problem... I have no troubles displaying any strings- its only the number ones that mess up by only returning "0" as the string. I don't have it set as "0" anywhere- I don't know why its displaying that number.
For testing purposes, what if you just try to echo the entered string itself without setting it to a value? (so mTextView.setText(mEditView.getText().toString())). (Also, is 0 just the value of an uninitialized int in Java? Try initializing it to 5 and observing the behavior).

Your Answer

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