views:

72

answers:

3

Brand new to droid programming, but would love to learn as much as possible, so I finally got my emulator working correctly, I even got a hello world button to work,

I'm attempting to make this button display a random number, I've googled this and came up with this code:

Random generator = new Random();
int n = generator.nextInt(n);

I fixed the Random function by including some Random java utility.

I'm assuming this code above goes in the .java file of the project, so my button code looks as follows (tested and works):

PopUpText.makeText(v.getContext(), "Hello World", 
PopUpText.LENGTH_LONG).show();

I figured I could replace "Hello World" with n to display the number in the box, however the following error is stopping the compile:

The local variable n may not have been initialized

Any ideas why this is happening? Any advice would be hugely appreciated.

A: 
Random generator = new Random();
int n = generator.nextInt(n);

you using the variable 'n' in its declaration, which is incorrect.

A correct code will read something like this

Random generator = new Random();
int n = 100;
n = generator.nextInt(n);
the100rabh
A: 

Well I figured it out by myself lol, this code works:

                Random generator = new Random();

                int n = generator.nextInt(10);

                PopUpText.makeText(v.getContext(), "Random Number: "+n, 
                        PopUpText.LENGTH_LONG).show();
Pete Herbert Penito
A: 

int n = generator.nextInt(n);
n isn't defined, yet

ItzWarty