Question & Answer: Write a short Java application that stores words in an Array or ArrayList. You get to pick the number of words to store…..

Write a short Java application that stores words in an Array or ArrayList. You get to pick the number of words to store. Generate a random number between 0 (inclusive) and the length of the Array or ArrayList (exclusive). Use this random number as an index into the Array or ArrayList. Display the value stored at the index.

Expert Answer

 

import java.io.*;
import java.util.*;

public class Demo2 {

public static void main(String[] args){

Scanner sc = new Scanner(System.in);
System.out.println(“Enter number of words to store:”);
int n = sc.nextInt();
String ar[] = new String[n];
for (int i = 0; i<n; i++){
ar[i] = “word” + Integer.toString(i);
}
Random rand = new Random();
int val = rand.nextInt(n);
System.out.println(“Word stored at index ” + val + ” is “+ ar[val]);
}
}

Still stressed from student homework?
Get quality assistance from academic writers!