Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Printing many random names from an array

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Printing many random names from an array

    In this code I create first a list with some names and then print them randomly:

    package testrandom;
     
     
    public class TestRandom {
     
     
    public static void main(String[] args) {
    String [] name={"Bob","Joe","Max","Alfred","Mat"};
    int min =0;
    int max= name.length;
    int random_int = (int)Math.floor(Math.random()*(max-min+1)+min);
    System.out.println(name[random_int]);
     
    }
     
    }

    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:

    package testrandom;
     
     
    public class TestRandom {
     
     
    public static void main(String[] args) {
    String [] name={"Bob","Joe","Max","Alfred","Mat"};
    int min =0;
    int max= name.length;
     
    for (int i=0; i<4;i++)
    ///System.out.println("Random value in int from " +min+"to "+max+" : ");
    int random_int = (int)Math.floor(Math.random()*(max-min+1)+min);
    //System.out.println(random_int);
    System.out.println(name[random_int]);
     
    }
     
    }
    Last edited by pooyan89; May 1st, 2021 at 12:56 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Printing many random names from an array

    failed
    Please explain what that means. What happened when the code was compiled and executed?
    Why did it fail?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    pooyan89 (May 1st, 2021)

  4. #3
    Junior Member
    Join Date
    May 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing many random names from an array

    Quote Originally Posted by Norm View Post
    Please explain what that means. What happened when the code was compiled and executed?
    Why did it fail?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Did it!

    My question is if I want a loop that each time I run the program it prints 3 names randomly from the list

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Printing many random names from an array

    What happened to the code's indentations? All the statements start in the first column. That makes the code harder to read and understand. For example it is hard to see what statements are inside of the for loop and what statements follow the loop.

    What happens when you compile and execute the code? Copy the output and paste it here. Add some comments to the output saying what is wrong with it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Printing many random names from an array

    Also posted here: https://coderanch.com/t/741926/java/...om-names-array
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Printing random values from array list.
    By toddy in forum Collections and Generics
    Replies: 4
    Last Post: June 16th, 2014, 01:05 PM
  2. multidimentional array- names & gender.
    By astro-one in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 19th, 2013, 09:36 PM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. [SOLVED] Help printing random dice
    By vanDarg in forum Java Theory & Questions
    Replies: 12
    Last Post: February 1st, 2011, 03:09 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM

Tags for this Thread