# Neeb question: returning an integer

• 02-24-2012, 06:20 PM
Humphrey Bogart
Neeb question: returning an integer
Hello. My apologies in advance for the seriously lame neebie question.

I know how to use Math.random() to get a random double (I think) between 0 - 1.
I know I can use Math.random() * 10 to generate a random double between 1 - 10.

But if I don't want a double and instead want an integer what do I do?
Using int Math.random() * 10 gets me an "unexpected argument list" error.

What is the correct syntax to turn a double into an integer?

And can anyone explain to me the difference between "int" and "integer"?

Thanks for your help and putting up with my stupid questions. hmb
• 02-24-2012, 06:32 PM
Fubarable
Re: Neeb question: returning an integer
You can simply cast the double to int via (int)myDouble. Or even better, use the Random class and its nextInt(10) method.

For your other question, int is a primitive number type, such as -10, -4, 0, 25, 30, 2000. You can't call methods on this since it is not an object.

Integer (note that the first letter is capitalized) is an "object wrapper" that is like int in that it holds an integer value, but as an object, you can call methods on it, and Integer variables can be null.
• 02-24-2012, 07:05 PM
Humphrey Bogart
Re: Neeb question: returning an integer
Hello. thanks so much for your reply. I appreciate your telling me the difference between int and Integer.

I want to create a 2D array of integers, populate it with random numbers between 1 - 10, and print it out. So I write this code, and get "not a statement" erors on compilation.

Code:

```public class jmp2 {     public static void main(String[] args) {                 int[][] rayray = new int[2][2];         int[0][0] = (int)Math.random() * 10;         int[0][1] = (int)Math.random() * 10;         for(int i = 0; i < rayray.length; i++) {             for(int j = 0; j < rayray[0].length; j++) {                 System.out.print(rayray[i][j])             }         }     } What am I doing wrong? Thanks again for your help.  hmb }```
• 02-25-2012, 12:01 AM
Fubarable
Re: Neeb question: returning an integer
This declares a variable named rayray that references a 2-dimensional int array:
Code:

`int[][] rayray = new int[2][2];`
Here you try to use the array, but not successfully:
Code:

`int[0][0] = ...`
Since int[0][0] doesn't make sense when used this way in Java. What you should be doing is using your variable here, not int since the [0][0] item in the array referenced by the rayray variable that you're trying to set. So better:
Code:

`rayray[0][0] = ...`