# Thread: Neeb question: returning an integer

1. Member Join Date
Mar 2011
Posts
93
Rep Power
0

## 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  Reply With Quote

2. ## 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.  Reply With Quote

3. Member Join Date
Mar 2011
Posts
93
Rep Power
0

## 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.

Java Code:
```public class jmp2 {
public static void main(String[] args) {

int[][] rayray = new int;
int = (int)Math.random() * 10;
int = (int)Math.random() * 10;
for(int i = 0; i < rayray.length; i++) {
for(int j = 0; j < rayray.length; j++) {
System.out.print(rayray[i][j])
}
}
}

What am I doing wrong?

Thanks again for your help.  hmb
}```  Reply With Quote

4. ## Re: Neeb question: returning an integer

This declares a variable named rayray that references a 2-dimensional int array:
Java Code:
`int[][] rayray = new int;`
Here you try to use the array, but not successfully:
Java Code:
`int = ...`
Since int doesn't make sense when used this way in Java. What you should be doing is using your variable here, not int since the  item in the array referenced by the rayray variable that you're trying to set. So better:
Java Code:
`rayray = ...`
Make sense. I strongly recommend you re-read your tutorials on how to use arrays as you will learn more about this and other useful aspects of these critters.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•