Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

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

    Default 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[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
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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[2][2];
    Here you try to use the array, but not successfully:
    Java 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:
    Java Code:
    rayray[0][0] = ...
    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.

Similar Threads

  1. Question about returning array??
    By kraigballa in forum New To Java
    Replies: 9
    Last Post: 01-27-2012, 07:02 AM
  2. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 09:59 PM
  3. Integer parseInt(str) question
    By prasads in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 11:10 PM
  4. neeb help Coding RADIO BUTTON for REMEMBER ME
    By timosoft in forum NetBeans
    Replies: 0
    Last Post: 02-04-2011, 10:54 AM
  5. fundamental question about Integer (for example)
    By kmansj in forum New To Java
    Replies: 10
    Last Post: 06-21-2010, 01:40 PM

Posting Permissions

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