-1

I was setting up a small app that asks a user to determine the array size and then populate it. The used "for" loop skips the index 0; but I'm uncertain why.

If you run this code with 1 as the array size it skips over the user inputting the first word.

The issue is certainly on the for-loop but it is so simple that I don't see it. Thanks!

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 = sc.nextInt();
    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]);




}

}

2 Answers2

1

Change your

arrayDimesion = sc.nextInt();

to

arrayDimesion = Integer.parseInt(sc.nextLine());

Reason: sc.nextInt() doesn't consume the newline character that you give after taking arrayDimesion input. This later on gets consumed in the next sc.nextLine() call.

PS: It might throw NumberFormatException. So you can handle it like :

try {
    arrayDimesion = Integer.parseInt(sc.nextLine());
} catch (NumberFormatException e) {
    e.printStackTrace();
}
Shanu Gupta
  • 3,333
  • 2
  • 15
  • 27
0

The below code is clean, easy to read and handles the edge cases.

import java.util.Scanner;

public class WordRandomizerAdvanced {

    public static void main(String[] args) {
        int numOfWords;
        Scanner scanner = 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?");
        numOfWords = Integer.parseInt(scanner.nextLine());
        String[] wordArray = new String[numOfWords];

        //Populate with user input
        System.out.println("Please enter the word(s)");
        for (int i = 0; i < numOfWords; i++) {
            wordArray[i] = scanner.nextLine();
        }

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

        //Print random string from array
        if (numOfWords == 0) {
            System.out.println("You didn't enter a word");
        } else {
            int r = (int) (Math.random() * numOfWords);
            System.out.println("The random word is: " + wordArray[r]);
        }
    }
}
Deepak Agrawal
  • 280
  • 1
  • 3
  • 14