1

I (beginner) want to print random names (string) from an array. Although this code is working just fine, I think this is not the right way to do this. Any help would be highly appreciated.

package com.test;

public class Main {

    public static void main(String[] args) {

        //print random names from the array

        String name[] = new String[10];
        name[0] = "John";
        name[1] = "James";
        name[2] = "Ashley";
        name[3] = "Robert";
        name[4] = "Bradley";
        name[5] = "Jennifer";
        name[6] = "Stewart";
        name[7] = "Natalie";
        name[8] = "Austin";
        name[9] = "Scarlette";

        int random = (int) (Math.random() * 9);
        switch (random) {
            case 0:
                System.out.println(name[0]);
                break;
            case 1:
                System.out.println(name[1]);
                break;
            case 2:
                System.out.println(name[2]);
                break;
            case 3:
                System.out.println(name[3]);
                break;
            case 4:
                System.out.println(name[4]);
                break;
            case 5:
                System.out.println(name[5]);
                break;
            case 6:
                System.out.println(name[6]);
                break;
            case 7:
                System.out.println(name[7]);
                break;
            case 8:
                System.out.println(name[8]);
                break;
            case 9:
                System.out.println(name[9]);
                break;}
    }
}
Alon Eitan
  • 11,798
  • 8
  • 44
  • 56
48731456
  • 39
  • 3
  • 3
    Absolutely not. Just do `System.out.println(name[random])`, you don't need the `switch` at all. – domsson Jul 12 '17 at 12:02
  • If it is working fine and you want your code reviewed.. you can check out https://codereview.stackexchange.com/ . – Suraj Rao Jul 12 '17 at 12:04
  • 1
    This question is more appropriate for the [Code Review stack exchange site](https://codereview.stackexchange.com/) – Ekaba Bisong Jul 12 '17 at 12:04
  • I'm voting to close this question as off-topic because working code should be reviewed in Code Review stack exchange site – Suraj Rao Jul 12 '17 at 12:05
  • Although I'm not sure the people over at *codereview* would be happy about this question either, @suraj – domsson Jul 12 '17 at 12:05
  • Possible duplicate of [How to print out a random String from an array in Java](https://stackoverflow.com/questions/16006449/how-to-print-out-a-random-string-from-an-array-in-java) – kleopi Jul 12 '17 at 12:06
  • And remember your random range is [0,9) i.e. 9 is excluded . So use 10 instead of 9 – anubhav deshwal Jul 12 '17 at 12:08
  • 1
    @suraj This would be just as terrible at Code Review. – Mast Jul 12 '17 at 12:08
  • fair enough.. But it would be off topic here since it is working code..@Mast – Suraj Rao Jul 12 '17 at 12:09
  • @suraj Irrelevant, don't refer people to sites if their question isn't on-topic there either. Creates horrible messes. – Mast Jul 12 '17 at 13:15
  • Please don't vandalize your post, you can delete it if you prefer (You have the "delete" link under the tags of the question) – Alon Eitan Aug 05 '17 at 06:58

2 Answers2

11

This would be a better way.

Remove the switch loop with

System.out.println(name[random]);

Also you can make the initialization and declaration part in a single line of code. Refer

And whole code will become

package com.test;
public class Main {

public static void main(String[] args) {

    String[] name = {"John","James","Ashley","Robert","Bradley","Jennifer","Stewart","Natalie","Austin","Scarlette"};
    //generating random number between 0 and name.length
    int random = (int) (Math.random() * name.length);

    //print random name from the array
    System.out.println(name[random]);

   }

}
Saranjith
  • 9,547
  • 2
  • 55
  • 102
0

You could use the nextInt(int) method of Java's Random class. It takes an integer as a parameter and returns a random integer that can range from 0 up to (but not including) your parameter. This makes it perfect for accessing a random index in an array.

Random random = new Random()    
System.out.println(name[random.nextInt(name.length)]);
Brian Risk
  • 1,006
  • 9
  • 17