-1

I am creating a simple dice rolling application.
I am getting this error:

cannot resolve method for my nextInt() method and valueOf() method.

I have declared the myResults variable as an Integer so I am not sure why I am getting this error. Currently the application should roll a die and return the value. I cannot get to the rest of the logic until I resolve this error. I want to make sure that I can at least get this part running before I continue. The end goal is to roll two die and return the sum of both but I have only coded for one die roll at the moment. Can someone point me in the right direction?

public class MainActivity extends Activity {

        // my variables
        TextView mText;
        ImageButton mButton;
        int myResults;
        String output;

        // function that runs on Start Up
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mText = (TextView)findViewById(R.id.results);
            mButton = (ImageButton)findViewById(R.id.roll);

            mButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    myResults = new Random(System.currentTimeMillis().nextInt(6)+1);
                    rollDice();
                    mText.setText(String.valueOF(myResults));
                }
            });
        }

        public void rollDice()
        {
            switch (myResults)
            {
                case 1:
                    mButton.setImageResource(R.drawable.die1);
                    break;
                case 2:
                    mButton.setImageResource(R.drawable.die2);
                    break;
                case 3:
                    mButton.setImageResource(R.drawable.die3);
                    break;
                case 4:
                    mButton.setImageResource(R.drawable.die4);
                    break;
                case 5:
                    mButton.setImageResource(R.drawable.die5);
                    break;
                case 6:
                    mButton.setImageResource(R.drawable.die6);
                    break;
            }
        }

Once this issue is resolved I would like some guidance on factoring in the second die into myResults. But resolving the methods first would be great.

Error at:

myResults = new Random(System.currentTimeMillis().nextInt(6)+1);
rollDice();
mText.setText(String.valueOF(myResults));
dsharew
  • 10,377
  • 6
  • 49
  • 75
NOSDUH
  • 111
  • 2
  • 12

4 Answers4

3

You placed the ) in the wrong place.

myResults = new Random(System.currentTimeMillis().nextInt(6)+1);

Should be

myResults = new Random(System.currentTimeMillis()).nextInt(6)+1;
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
2

System.currentTimeMillis() returns a long which is a primitive and hence doesn't have any member nextInt(...).

valueOF() << I think your problem here is the capitalised 'F'.

On a somewhat related note I wouldn't include the dice logic in the activity code. A better approach that follows the principles of OOP and MVC architecture would be to extract the dice logic into a separate class and call that from the activity.

James S
  • 308
  • 1
  • 7
2

String.valueOf() is incorrectly capitalised in your code (String.valueOF()).

You are also using the Random nextInt() method incorrectly. System.currentTimeInMillis() returns a long value, whereas nextInt() requires a Random object. The correct way to use nextInt() is demonstrated in the answer to this question.

Random r = new Random();
int i1 = r.nextInt(80 - 65) + 65;
Community
  • 1
  • 1
PPartisan
  • 8,173
  • 4
  • 29
  • 48
1

The problem is,

First:

myResults = new Random(System.currentTimeMillis()).nextInt(6)+1;

Second:

mText.setText(String.valueOf(myResults));

Try this way.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198