Results 1 to 9 of 9

Thread: RandomGenerator

  1. #1
    Anaconda is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default RandomGenerator

    Hi,

    I was trying to use the RandomGenerator in this way:
    private RandomGenerator rgen = RandomGenerator.getInstance();

    but I got this error:
    "The method getInstance() is undefined for the type RandomGenerator"

    Then I just used:
    private RandomGenerator rgen = new RandomGenerator();

    and it worked fine.

    Could anybody tell me what is the difference? and why it didnīt work in the first way?

    Thanks, Anaconda.

  2. #2
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    i believe it is because when u used the getInstace() it was just doing that function and when u used the other one it covered all of the functions in the randomgenerator im pretty sure

    but i could be wrong
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Java Code:
    private RandomGenerator rgen = RandomGenerator.getInstance();
    Calls the (static) getInstance method in RandomGenerator. If the method doesn't exist, a compile error occurs.

    Java Code:
    private RandomGenerator rgen = new RandomGenerator();
    Calls the no-arg constructor in RandomGenerator. Since this compiles for you, you must have declared a no-arg constructor in your class. Either that or you declared no constructors. In the latter case, Java automatically provides a default no-arg constructor.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  4. #4
    Anaconda is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks to both.

    However, the RandomGerator class is supposed to have a getInstance method in it, as specified in the documentation of the acm.util.*.

    And I do have the import statement:
    import acm.util.*;

    I still wonder why the compiler does not recognize it.

  5. #5
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by Anaconda View Post
    Thanks to both.

    However, the RandomGerator class is supposed to have a getInstance method in it, as specified in the documentation of the acm.util.*.

    And I do have the import statement:
    import acm.util.*;

    I still wonder why the compiler does not recognize it.
    It works for me.

    Java Code:
    import acm.util.*;
    
    public class Test
    {
    	/**
    	 * Main method
    	 * 
    	 * @param args
    	 *            (not used)
    	 */
    	public static void main(String[] args)
    	{
    		RandomGenerator rgen = RandomGenerator.getInstance();
    		
    		System.out.println(rgen.nextInt(1, 6));
    	}
    }
    Also, next time you refer to an external library, please post a URL where we can look it up. I thought you were having a problem with one of your classes, but it ends up you are having problems with someone else's code. By providing a link, we could have downloaded the library, and checked for ourselves, getting you the answer you seek faster.


    Can you post your code, since I got the library to work fine. Also, try downloading the latest copy of the library.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  6. #6
    Anaconda is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks for your help, I am very sorry for not having posted the library link. The thing is that I am new in this forum, and when I tried to post the link it didn't allow me, because I have less than 20 posts.
    However, I think I found the problem, I was trying to run a program from the Stanford course Programming Methodology. But I copied the code in a working project (folder) without realizing that it didn't have the acm.jar file, neither it was included in the build path. As you may imagine I am a beginner...
    Once I realized this, everything worked as expected.

    Again, thanks for your help CodesAway.

  7. #7
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    I'm glad you figured it out.


    Even though you can't post a clickable link, I think you can still post a link - just leave off the "http://" part.

    For example, "Check the library out at jtf.acm.org/" should work. Or, "I'm using an external library, search for 'jtf acm'"

    I'm not nagging you, just some helpful advice for next time.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  8. #8
    ahmednasir is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default

    I am also having same problem, but I dont understand the solution can you please explain me how did you resolve it..

  9. #9
    Anaconda is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Hi ahmednasir,

    If you are using Eclipse, you need to include the acm.jar file in the building path of the project, this is the file which implement the randomgenerator code. Right click on the project folder in the Eclipse window, then click on properties, and add the acm.jar file to the Libraries tab.
    You can dwload the file from: jtf.acm.org

    Hope this helps.

Similar Threads

  1. RandomGenerator.getInstance()
    By dlevanchuk in forum New To Java
    Replies: 1
    Last Post: 11-27-2009, 06:32 AM

Posting Permissions

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