[FIXED] How to limit the number of words when reading a line from standard input?


I am new to Stackoverflow and this is my first time asking a question. I have searched my problem thoroughly, however, could not find an appropriate answer. I am sorry if this has been asked. Thank you in advance.

The question is from Hyperskill.com as follows:

Write a program that reads five words from the standard input and outputs each word in a new line.
First, you need to print all the words from the first line, then from the second (from the left to right).

Sample Input 1:

This Java course
is adaptive

Sample Output 1:


My trial to solve it

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        /* I have not initialized the "userInput" String.
         * I know that String is immutable in Java and
         * if I initialize it to an empty String ""
         * and read a String from user.
         * It will not overwrite to the "userInput" String.
         * But create another String object to give it the value of the user input,
         * and references the new String object to "userInput".
         * I didn't want to waste memory like that.
        String userInput;
        String[] userInputSplitFirstLine = new String[3];
        String[] userInputSplitSecondLine = new String[2];
        Scanner scan = new Scanner(System.in);
        userInput = scan.nextLine();
        userInputSplitFirstLine = userInput.split("\\s+");
        userInput = scan.nextLine();
        userInputSplitSecondLine = userInput.split("\\s+");
        for(String firstLineSplitted: userInputSplitFirstLine) {
        for(String secondLineSplitted: userInputSplitSecondLine) {

If you try the sample input above, the output will match the sample output above. However, if you write more than 3 words to the first line and/or more than 2 words to the second line, the userInputSplitFirstLine array of size 3 will store more than 3 words. Same goes with the userInputSplitSecondLine array also. My first question is how can an array of size 3 (userInputSplitFirstLine) and an array of size 2 (userInputSplitSecondLine) can hold more than 3 and 2 elements, respectively? My second question is that how can I restrict/limit the number of words that the user can insert in a line; for example, the first line only accepts 3 words and the second line only accepts 2 words?

Also the answer to this question suggested by Hyperskill.com is as follows:

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String wordOne = scanner.next();
        String wordTwo = scanner.next();
        String wordThree = scanner.next();
        String wordFour = scanner.next();
        String wordFive = scanner.next();


I think this should do the work. Don’t hesitate to ask, if you have some questions.

import java.util.Scanner;

class App {
    public static void main(String[] args) {
        final StringBuffer line = new StringBuffer();
        final StringBuffer words = new StringBuffer();

        try (final Scanner sc = new Scanner(System.in)) {
            while (sc.hasNextLine()) {
                final String currentLine = sc.nextLine();
                for (final String word : currentLine.split("\\s+")) {
        } finally {

Answered By – h43ckm43ck

Answer Checked By – Katrina (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published