aspose file tools*
The moose likes Java in General and the fly likes problem with getting random element out of array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem with getting random element out of array" Watch "problem with getting random element out of array" New topic
Author

problem with getting random element out of array

kevin cash
Greenhorn

Joined: Nov 01, 2012
Posts: 1
i am trying to generate a random element out of an array. I have assigned the array to reference variable s1, i have declared a random r1 and know to use a fore loop with the array length but I cant use the reference variable in the fore loop . any when I use the the original array studentArray its empty.


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

When you post code, there are some things you can to do make it easier to read, which in turn makes it more likely someone will be willing and able to help you.

  • UseCodeTags(←click). I've added them for you in your initial pots.
  • Don't use so much whitespace, and be consistent in how you do use it. A single blank line between methods, or between logical sections within a method is usually sufficient. I've cleaned up some of that for you as well.
  • Use consistent indentation. 2 or 4 spaces, not tabs. I didn't try to touch that hot mess.
  • Post everything needed to demonstrate your problem, and nothing that's not needed. That is, an SSCCE(←click). There's a lot of code there that's not relevant to your problem. Even though it's not clear to me what your actual problem is, I guarantee you don't need all that code to demonstrate it.


  • I know this may sound like I'm just nitpicking unimportant stuff instead of helping you, but believe me, clearly and precisely communicating your problem so that it's easy for folks here to understand it is paramount if you want help.

    So, can you explain a bit more clearly what you're having trouble with, and provide a more concise code sample to demonstrate it?

    Mike Simmons
    Ranch Hand

    Joined: Mar 05, 2008
    Posts: 2970
        
        9
    I agree with Jeff on all points. I tried to answer the question anyway, but couldn't.

    kevin cash wrote:I have assigned the array to reference variable s1, i have declared a random r1 and know to use a fore loop with the array length but I cant use the reference variable in the fore loop .

    I see a couple places where you declare a Random r1, but I don't see any attempt to use r1 in a for loop. It should be possible, so if it's not working for you, show us what you're doing that's not working.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    I'll also point out that if you want to get a random element from an array, there's no need to loop. It's just two steps. 1. Generate the random index. 2. Retrieve the element at that index.

    Kemal Sokolovic
    Bartender

    Joined: Jun 19, 2010
    Posts: 825
        
        5

    int rand = r1.nextInt(numberOfStudents - 1); would get you a random index and you would just get value (student) from an array by using that same index.

    Few more things:
  • You initialize your array to contain 10 elements, and then you aks user to provide number of students to put in that same array? What if the user enters 1 (9 empty) or 20 (what then??)?
  • Do you really need setNumberOfStudents() method if you are counting that number on each instance creation?
  • You should really follow naming convention when writing code. What you did looks very bad.


  • The quieter you are, the more you are able to hear.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36579
        
      16
    Welcome again

    Before you use that technique, read the documentation for the Random class very carefully, and work out what the - 1 bit will do.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: problem with getting random element out of array
     
    Similar Threads
    Need help with program.
    Explain Working of compareTo() method with respect to this example
    getFirstName from one class (set & get) to another class (Main Program)
    inheritance homework assignment - im stuck
    Arrays: Calling input from other methods and classes