Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with getting random element out of array

 
kevin cash
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?

     
    Mike Simmons
    Ranch Hand
    Posts: 3090
    14
    • Mark post as helpful
    • send pies
    • 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
    • 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.

     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • 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.
  •  
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49443
    62
    • Mark post as helpful
    • send pies
    • 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic