Results 1 to 10 of 10
  1. #1
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Question random to take variables

    how would i go about having a random number up to a variable:
    Java Code:
    Random ran = new Random();
    long max = 100;
    int num = ran.nextInt(max); //this is how i did it
    ok, so when i do this, it just returns that there is no such symbol (i.e. method) as ran.nextInt(),

    so people here are usually good help :) could you tell me the real way to do it (without infinite if/else if/else statements!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    nextInt wants an integer, not a long! -> int max = 100;

  3. #3
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default

    oh?? sorry i had read somewhere that random takes long, but thank you

  4. #4
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default wait...

    still getting this error:
    Java Code:
    mynum2.java:17: cannot find symbol
    symbol  : method nextInt(long)
    location: class java.util.Random
                int num1 = ran.nextInt(max);
                              ^
    1 error
    ??????

  5. #5
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default

    wait... solved thank you

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,619
    Rep Power
    25

    Default

    Reading the API doc will often explain these kinds of errors.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,682
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by javajames View Post
    still getting this error:
    Java Code:
    mynum2.java:17: cannot find symbol
    symbol  : method nextInt(long)
    location: class java.util.Random
                int num1 = ran.nextInt(max);
                              ^
    1 error
    ??????
    Actually reading the error message would've solved your problem: you're still trying to call the nextInt( ... ) method with a parameter of type long.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default

    you still posted that despite the fact i had mentioned i had solved it. I had to wade through the program to find the place were i had entered long, i didn't find that until later on, when i realized that i was using that random in a function i had created that took max, it hit me that i had to change the input variables of the function to work with integers. So yes, i did read the API, but i just didn't see the problem until later on, but even though i posted that i had solved it, you still batter me with sarcasm that i don't read the build errors!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,682
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by javajames View Post
    you still posted that despite the fact i had mentioned i had solved it. I had to wade through the program to find the place were i had entered long, [ snip ... ]
    The compiler told you the line number; you should've read it. You still have a lot to learn.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default

    yes, that is true, i will certainly admit that, seeing as i started java 1 week ago (you may remember answering some of my previous queries! :)

Similar Threads

  1. Replies: 17
    Last Post: 07-25-2010, 08:20 PM
  2. Replies: 14
    Last Post: 10-19-2009, 11:57 AM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 PM
  4. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM

Tags for this Thread

Posting Permissions

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