2

I made an array, but I confused as how to dispaly the array contents depanding on a random generator.

E.g. If there are four statements in my array, and when I run the program I get number 3, I would like it to link to a statment in the array.

E.g. its should print out: 3 - Get a job

I'm using this for the array:

String[] activityName = new String[5];     
    activityName[1] = "Go and Have Fun";
    activityName[2] = "Hiya, Go Shooping";
    activityName[3] = "Get a job";
    activityName[4] = "Read a book";

And I am using this code to gerenate a random number:

 Random generator = new Random();
    int n=5;
         int randomNumber = generator.nextInt( n );
    System.out.println(randomNumber);

Plus I don't want to use a 0 in the generator.

Any help would be appreciated. Thank you.

| |
  • If the only problem is that you don't want to have a 0 in the generator, you should just take n = 4 and then add one to randomNumber. – Sören Mar 2 '11 at 15:13
5

always index arrays in java from 0-index. Arrays in Java are zero-indexed. You can simply add 1 to your generated randomNumber. For getting activity name see samble below which is safe even if you remove/add elements to your array.

    String[] activityName = new String[4];     
    activityName[0] = "Go and Have Fun";
    activityName[1] = "Hiya, Go Shooping";
    activityName[2] = "Get a job";
    activityName[3] = "Read a book";

    Random generator = new Random();
    int randomNumber = generator.nextInt(activityName.length);
    System.out.println(activityName[randomNumber]);
| |
2

If I understand correctly, then you would just say:

System.out.println(activityName[randomNumber]);
| |
  • I could not recommend this because you can get randomNumber=0 and then return null from array in this case. – michal.kreuzman Mar 2 '11 at 15:18
0

The following code should do what your looking for:

System.out.println(activityName[1 + new Random().nextInt(4)]);
| |
0

I am going to add a bit to dmcnelis' answer and say that if you really don't want to use the 0th index for whatever reason you should do this...

/* only select a random number in a range of 4 and add 1 to be sure it is > 0 */
int randomNumber = generator.nextInt( activityName.length - 1 ) + 1;
System.out.println(activityName[randomNumber]);
| |
0

Given that you don't want to use the zero index, maybe you should try using a HashMap. A code example is shown below that generates 10 successive random choices.

The HashMap is part of the Java Collections Framework, which gives you access to a powerful set of methods.

You can extend the example, simply by using the "put" method to add more choices to the hashmap. This example does rely on you adding successive numbers from 1..x without any gaps.

HashMap<Integer, String> activityName = new HashMap<Integer, String>();
activityName.put(new Integer(1), "Go and Have Fun");
activityName.put(new Integer(2), "Hiya, Go Shooping");
activityName.put(new Integer(3), "Get a job");
activityName.put(new Integer(4), "Read a book");

Random generator = new Random();
int n=activityName.values().size();

int randomNumber;
for (int i = 0; i < 10; i++) {

    //nextInt() gets numbers from 0(included) to n(excluded), so need to add 1
    randomNumber = generator.nextInt(n) + 1;

    //Cast the retured Object to a string
    String activity = new String(activityName.get(new Integer(randomNumber)));
    System.out.println(randomNumber + " - " + activity);
}
| |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.