Answers for "populate array from user input"

0

populate array from user input

import java.util.Scanner;

public class WordRandomizerAdvanced {

public static void main(String[] args) {


    int arrayDimesion;

    Scanner sc = new Scanner(System.in);

    System.out.println("****************************************************");
    System.out.println("******** Welcome to Word Randomizer ADVANCED********");
    System.out.println("****************************************************");

    //Get array size
    System.out.println("How many words would you like to enter?");
    arrayDimesion = Integer.parseInt(sc.nextLine());
    String[] wordArray = new String[arrayDimesion];


    //Populate with user input
    for (int i=0; i<arrayDimesion; i++) {

    System.out.println("Please enter a word"); 
    wordArray[i] = sc.nextLine();
    }


    //Print all entered Strings     
    System.out.println("This are the words you entered: ");
    for(int i = 0; i < wordArray.length; i++) {
        System.out.println(wordArray[i]);
    }

    //Print random string from array
    int r = (int)(Math.random() * wordArray.length);
    System.out.println("The random word is: " + wordArray[r]);




}

}
Posted by: Guest on April-17-2022

Code answers related to "populate array from user input"

Browse Popular Code Answers by Language