• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with getting random element out of array

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

     
    Master Rancher
    Posts: 4806
    72
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.

     
    Bartender
    Posts: 825
    5
    Python Ruby Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
  •  
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic