0

I'm making a simple dice rolling app as a way to learn some basics about Android Studio.

The method called onClick is shown below:

public void setSide(View v){
    Button b = (Button)v;
    sides = Integer.getInteger(b.getText().toString(),-1);
}

It seems to be passing -1 to sides no matter what, and all the buttons calling this method have only numbers in their strings (ex: "2", "20", "100", etc). Is there a way to fix this?

EDIT: I should probably explain that sides is local, and is used in another method, doRoll which is called the the Click of another button. This method sets the text of a textview to roll(sides), a method which returns a number from 1-sides if sides > 1, otherwise it returns -999. I have sides initialized to 20, and when I press the rollBtn before anything else, it works fine.

3

5 Answers 5

1

Try like this

sides = Integer.parseInt(b.getText().toString());

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

5 Comments

This works, but I'm still getting an error... I think it may be in the displaying of the roll. It warned me when I tried to use (Integer)(roll(sides)).toString(), and I'm not sure how to use the String.format(params) method. Could you tell me how to make it so the format() method prints just an integer?
the popup Android Studio gives me one with String locale, Object... args and another with String format, String locale, Object... args I'm not sure which one to use
Can you explain more clearly what and in what form you want to format
I just want to put an int in a TextView. When I tried to use the Integer.toString() method, I got a warning saying not to do so, and to use the String.format() method instead, but I'm not sure how to use it, as I've never worked with it before.
for that purpose use String.valueOf() method
1

You can use this:

sides = Integer.parseInt(b.getText().toString());

Comments

0

Try this and let me know:

public void setSide(View v){
Button b = (Button)v;
sides = Integer.parseInt(b.getText().toString());
}

Comments

0

check with only one condition :-

if(!TextUtils.isEmpty(b.getText().toString())){
    sides = Integer.getInteger(b.getText().toString());
} else {
    sides = -1;
}

Comments

0
sides = Integer.parseInt(button.getText().toString());

it works for me

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.