Here's a simple code that reads strings from stdin, adds them into List<String>, and then uses toArray to convert it to String[] (if you really need to work with arrays).

import java.util.*;

public class UserInput {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        Scanner stdin = new Scanner(System.in);

        do {
            System.out.println("Current list is " + list);
            System.out.println("Add more? (y/n)");
            if (stdin.next().startsWith("y")) {
                System.out.println("Enter : ");
                list.add(stdin.next());
            } else {
                break;
            }
        } while (true);
        stdin.close();
        System.out.println("List is " + list);
        String[] arr = list.toArray(new String[0]);
        System.out.println("Array is " + Arrays.toString(arr));
    }
}

See also:

  • Why is it preferred to use Lists instead of Arrays in Java?
  • Fill a array with List data
Answer from polygenelubricants on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-take-array-input-from-user-in-java
How to Take Array Input From User in Java? - GeeksforGeeks
July 23, 2025 - Explanation: In this example, first we use the Scanner class to accept user input and save the array size in the size variable. Then we make an array arr with the size s. Following that, we use a for loop to accept user input and save it in an array.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2024 โ€บ 06 โ€บ how-to-take-array-input-in-java
How to take array input in Java
import java.util.Scanner; public class TwoDArrayInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows = scanner.nextInt(); System.out.print("Enter the number of columns: "); int cols = scanner.nextInt(); int[][] array = new int[rows][cols]; System.out.println("Enter the elements of the array:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { array[i][j] = scanner.nextInt(); } } // Print the array System.out.println("Array elements:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { System.out.print(array[i][j] + " "); } // Move to the new line after printing each row System.out.println(); } scanner.close(); } }
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 07 โ€บ how-to-take-array-input-in-java-using-Scanner-Example.html
How to take array input from command line in Java ? Scanner Example
April 2, 2025 - That's all about how to take array input in Java using Scanner class. It's a simple technique to save input from the user into an array and can be used to save input in both one and multi-dimensional arrays.
๐ŸŒ
NavTutorial
navtutorial.com โ€บ home โ€บ code snippets โ€บ how to take array input from user in java
How to Take Array Input From User in Java (Scanner, 2026)
September 29, 2026 - Always wrap user-facing input logic in validation loops (while loops that re-prompt on invalid input) for production-quality command-line tools. A main method that reads from the keyboard can't be tested automatically. Move the logic into a method that takes a Scanner โ€” in the program it reads System.in, in a test it reads a string: import java.util.Scanner; public class ArrayReader { /** Reads a size followed by that many integers.
๐ŸŒ
Learninglad
learninglad.com โ€บ example program โ€บ java programming
User Input for an Array in Java Programming | LearningLad.com
You will learn how to create an array, how to read input for the all array elements from user, how to display all the all array elements using a loop in detail with example. ... package learningJava; import java.util.Scanner; public class Apples { public static void main(String[] args) { int length; Scanner input = new Scanner(System.in); System.out.println("How many friends name that you are gonna enter"); length = input.nextInt(); String[] names = new String[length]; for(int counter = 0; counter < length; counter++){ System.out.println("Enter the name of friend "+(counter+1)); names[counter] = input.next(); } input.close(); System.out.println("Your friends are"); for(int counter = 0; counter < length; counter++){ System.out.println(names[counter]); } } }
Top answer
1 of 9
7

Here's a simple code that reads strings from stdin, adds them into List<String>, and then uses toArray to convert it to String[] (if you really need to work with arrays).

import java.util.*;

public class UserInput {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        Scanner stdin = new Scanner(System.in);

        do {
            System.out.println("Current list is " + list);
            System.out.println("Add more? (y/n)");
            if (stdin.next().startsWith("y")) {
                System.out.println("Enter : ");
                list.add(stdin.next());
            } else {
                break;
            }
        } while (true);
        stdin.close();
        System.out.println("List is " + list);
        String[] arr = list.toArray(new String[0]);
        System.out.println("Array is " + Arrays.toString(arr));
    }
}

See also:

  • Why is it preferred to use Lists instead of Arrays in Java?
  • Fill a array with List data
2 of 9
7
package userinput;

import java.util.Scanner;

public class USERINPUT {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        //allow user  input;
        System.out.println("How many numbers do you want to enter?");
        int num = input.nextInt();

        int array[] = new int[num];

        System.out.println("Enter the " + num + " numbers now.");

        for (int i = 0 ; i < array.length; i++ ) {
           array[i] = input.nextInt();
        }

        //you notice that now the elements have been stored in the array .. array[]

        System.out.println("These are the numbers you have entered.");
        printArray(array);

        input.close();

    }

    //this method prints the elements in an array......
    //if this case is true, then that's enough to prove to you that the user input has  //been stored in an array!!!!!!!
    public static void printArray(int arr[]){

        int n = arr.length;

        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }

}
๐ŸŒ
Talkerscode
talkerscode.com โ€บ howto โ€บ how-to-take-array-input-from-user-in-java.php
How To Take Array Input From User In Java - TalkersCode.com
We import the classes Arrays and Scanner from the java.util package at the beginning of the code. The toString() function used in the programme is one of the methods available in the Arrays class for working with arrays. The Scanner class offers ...
๐ŸŒ
Post.Byes
post.bytes.com โ€บ home โ€บ forum โ€บ topic โ€บ java โ€บ how do i create an array using user input?
How do I create an array using user input? - Post.Byes - Bytes
But now I have to find out just what this scanner thing is. Do you have any links of where I could find out more about it? Thanks in advance! ... Scanner class allows you to take input from the keyboard. You can get a little bit information about Scanner class by just typing java.util.Scann ...
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 69267035 โ€บ how-to-display-values-in-array-and-take-user-input-in-array-using-java
how to display values in Array AND take user input in Array using java? - Stack Overflow
import java.util.Scanner; public ...println(array[i]); } } } You need to create the array of your required type and size then you have to get the inputs using a for loop for array elements....
๐ŸŒ
Just Academy
justacademy.co โ€บ blog-detail โ€บ how-to-take-array-input-in-java
How to Take Array Input in Java by Roshan Chaturvedi | JustAcademy
To Download Our Brochure: https://www.justacademy.co/download-brochure-for-free ... 3) After creating the array, you can use a for loop to iterate through each element and take input from the user for each element.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 07 โ€บ how-to-take-array-input-in-java-using-Scanner-Example.html
Javarevisited: How to take array input from command line in Java ? Scanner Example
That's all about how to take array input in Java using Scanner class. It's a simple technique to save input from the user into an array and can be used to save input in both one and multi-dimensional arrays.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-populate-an-array-one-value-at-a-time-by-taking-input-from-user-in-java
How to populate an array one value at a time by taking input from user in Java?
February 19, 2020 - To read data from user create a scanner class. Read the size of the array to be created from the user using nextInt() method. Create an array with the specified size. In the loop read the values from the user and store in the array created above.
๐ŸŒ
CodeChef
codechef.com โ€บ learn โ€บ course โ€บ java โ€บ JAVANEW24 โ€บ problems โ€บ XWZZRQ01B
Taking Array as Input in Java
Test your Learn Java knowledge with our Taking Array as Input practice problem. Dive into the world of java challenges at CodeChef.
๐ŸŒ
Medium
medium.com โ€บ @meenakshisinha121 โ€บ how-to-take-input-array-in-java-0657ad24f391
how to take input array in java. how to take input array in java | by Meenakshi From JustAcademy | Medium
April 25, 2024 - In Java, you can take input for an array by first specifying the size of the array and then reading each element of the array one by one. You can use a Scanner object from the java.util package to read input from the user.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How To Get Array Input From A User In Java Using Scanner - YouTube
$1,500 OFF ANY Springboard Tech Bootcamps with my code ALEXLEE1500. See if you qualify for the JOB GUARANTEE! ๐Ÿ‘‰ https://bit.ly/3HX970hIn this video, I show ...
Published: March 30, 2023
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 69266770 โ€บ take-input-for-array-from-user-in-java
take input for array from user in java - Stack Overflow
September 21, 2021 - ... You can read the array as a string like : 1 2 3 4 ..., and then extract and cast the values. Or you can ask the user for the size of the array (number of elements that the user wants to insert ) and then loop to scan and save each element.
๐ŸŒ
CodeChef
codechef.com โ€บ learn โ€บ course โ€บ java-development โ€บ AKYRVN โ€บ problems โ€บ XWZZRQ01B
Taking Array as Input in Java for Project building
import java.util.Scanner; class Codechef { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Create a Scanner object int[] marks = new int[5]; // Step 1: Declare an array of size 5 // Step 2: Take input for each element System.out.println("Enter marks of 5 students:"); for (int i = 0; i < marks.length; i++) { marks[i] = sc.nextInt(); // Read each mark from user, let's suppose input is 50,60,70,80,90 } // Step 3: Print the entered marks System.out.println("You entered:"); for (int i = 0; i < marks.length; i++) { System.out.println("Student " + (i + 1) + ": " + marks[i]); } sc.close(); // Close the scanner } } Output: You entered: Student 1: 50 Student 2: 60 Student 3: 70 Student 4: 80 Student 5: 90 ยท
Top answer
1 of 6
12

For future reference, here's a way to do it with the Scanner class:

import java.util.Arrays;
import java.util.Scanner;


public class NumberReading {
    public static int [] readNumsFromCommandLine() {

        Scanner s = new Scanner(System.in);

        int count = s.nextInt();
        s.nextLine(); // throw away the newline.

        int [] numbers = new int[count];
        Scanner numScanner = new Scanner(s.nextLine());
        for (int i = 0; i < count; i++) {
            if (numScanner.hasNextInt()) {
                numbers[i] = numScanner.nextInt();
            } else {
                System.out.println("You didn't provide enough numbers");
                break;
            }
        }

        return numbers;
    }

    public static void main(String[] args) {
        int[] numbers = readNumsFromCommandLine();
        System.out.println(Arrays.toString(numbers));
    }
}
2 of 6
5

You said you could use the following code in C++:

for(i=0;i<6;i++) 
cin>>a[i];

If this is what you used in C++, why not just use this?

for(i=0;i<6;i++)
a[i] = input.nextInt();

Assuming that the scanner declaration was:

Scanner input = new Scanner(System.in);

From what I've seen, people are trying to write the entire program for you. But seeing as you stated you could solve this problem using the C++ code, I just converted the code to Java for you.

If you want the full code, here it is:

import java.util.Scanner;

public class arrayFun {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // Create a new array. The user enters the size
        int[] array = new int[input.nextInt()];

        // Get the value of each element in the array
        for(int i = 0; i < array.length; i++)
            array[i] = input.nextInt();

        /* Note that the user can type the following:
         * 6
         * 34 12 7 4 22 15
         * This will set the array to size 6
         * array[0] = 34;
         * array[1] = 12;
         * etc.
         */

    }

}

I realize that this response is years late, but I was searching for a different question when I ran across this. The answers here were very confusing to me until I realized this wasn't what I was looking for. Seeing as this still shows up on google, I decided to post this.

๐ŸŒ
Just Academy
justacademy.co โ€บ blog-detail โ€บ how-to-get-array-input-in-java
How to Get Array Input in Java by Roshan Chaturvedi | JustAcademy
1 - Declare an array variable with a specific data type to store the input values provided by the user. 2) Use the Scanner class from the java.util package to read the user input from the console.