Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Printing many random names from an array

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this code I create first a list with some names and then print them randomly:



This code prints only one name everytime I run it. Now if I want to have for exemple 3 random names from that list each time, how should I modify my code ?!

I tried with this but failed:

 
Rancher
Posts: 582
9
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you want this line do?
 
Pouyan Msgn
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:What do you want this line do?



Creating a random number
 
Saloon Keeper
Posts: 8095
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You need to enclose the random and print part inside braces ({...}).

Math.random is almost never used anymore and has been replaced with the Random class which has a nextInt() method which should serve your purpose, and it will make the code cleaner.

Your min, max calculation is wrong.

The Java naming convention says the underscore (_) shouldn't be used except for constant names. Instead, use camel case. So, "random_int" should be "randomInt".
 
Carey Brown
Saloon Keeper
Posts: 8095
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think part of your confusion about the use and placement of the braces comes from the lack of indentation in your code. The main() method should be indented 1 level, the body of main() another level, and the body of the for-loop yet another. Clean that up and it may be clearer.

Note that an indentation level should be represented by 4 space characters.
 
Carey Brown
Saloon Keeper
Posts: 8095
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example indentation:
This brace style is called Alman which I find preferable for beginners. It is much easier to find missing or extra braces with this style.
 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pouyan Msgn wrote:. . . Creating a random number

Not quite. As Carey suggests, arithmetic with Math.random() isn't a good idea; it is very error‑prone. And as he suggested, you can use a method of the Random object to get exactly the number you want in the range you want.
 
Master Rancher
Posts: 4284
38
 
I didn't say it. I'm just telling you what this tiny ad said.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic