Examples
Here's an example to illustrate the process:
Java
import java.util.Scanner;
public class ArrayInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5]; // An array to hold 5 integers
// Taking input for the array
for(int i = 0; i < numbers.length; i++) {
System.out.println("Enter number " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
}
// Displaying the array elements
System.out.println("You entered: ");
for(int num : numbers) {
System.out.println(num);
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Enter number 1:
5
Enter number 2:
5
Enter number 3:
7
Enter number 4:
9
Enter number 5:
10
You entered:
5
5
7
9
10
In this example, the program prompts the user to enter five numbers, stores them in an array, and then prints out the numbers. It's a simple yet effective way to understand how array inputs work in Java.
Using BufferedReader & InputStreamReader Class
When you need to handle larger inputs or want to optimize performance, the BufferedReader along with InputStreamReader comes into play. This combination provides a more efficient way to read text from the input stream. Let's see how we can use these classes to take array input in Java.
Import Necessary Classes
First, ensure your program knows about the BufferedReader and InputStreamReader by importing them.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
Create BufferedReader Instance
To read data, create an instance of BufferedReader. This is done by wrapping an InputStreamReader, which in turn is created by System.in, the standard input stream.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Define Your Array: Just like before, you need an array. Decide its type and size based on your needs.
int[] myArray = new int[5]; // Array to hold 5 integers
Read & Store Data
Use a loop to read data from the console and store it in your array. BufferedReader reads data line by line, so you'll need to convert the input to the appropriate type, like Integer in this case.
for(int i = 0; i < myArray.length; i++) {
try {
System.out.println("Enter a number: ");
myArray[i] = Integer.parseInt(reader.readLine()); // Reading and storing data
} catch (IOException e) {
e.printStackTrace(); // Handling potential IO exceptions
}
}
Example
Here's a full example that demonstrates how to use BufferedReader and InputStreamReader to input data into an array:
Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ArrayInputExample {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int[] numbers = new int[5]; // An array to hold 5 integers
// Taking input for the array
for(int i = 0; i < numbers.length; i++) {
try {
System.out.println("Enter number " + (i + 1) + ": ");
numbers[i] = Integer.parseInt(reader.readLine());
} catch (IOException e) {
System.out.println("An error occurred. Please try again.");
e.printStackTrace();
}
}
// Displaying the array elements
System.out.println("You entered: ");
for(int num : numbers) {
System.out.println(num);
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Enter number 1:
5
Enter number 2:
5
Enter number 3:
7
Enter number 4:
9
Enter number 5:
10
You entered:
5
5
7
9
10
In this program, the user is prompted to enter five numbers. These numbers are then stored in an array using the BufferedReader and InputStreamReader classes. Finally, the numbers are printed out to demonstrate that they were stored correctly.
This method is particularly useful for reading large amounts of data efficiently. While it might seem a bit more complex due to the need to handle exceptions, it's a valuable tool in your Java toolkit.
Frequently Asked Questions
Why use BufferedReader over Scanner for reading array input in Java?
BufferedReader can handle larger inputs more efficiently than Scanner because it reads data in bulk (larger blocks) rather than parsing it token by token. This can significantly improve performance in input-intensive applications.
How do you handle exceptions when taking array input in Java?
When using BufferedReader, it's common to encounter IOExceptions due to input/output errors. You should surround your input code with a try-catch block to handle these exceptions gracefully, ensuring your program doesn't crash unexpectedly.
Can you use these methods for types other than int?
Absolutely! The examples provided focus on integer arrays, but you can adapt the code to work with any data type. For non-integer types, adjust the parsing method in the input section accordingly (e.g., use Double.parseDouble for double arrays).
Conclusion
In this article, we have learned two primary methods for taking array input in Java, focusing on using the Scanner class with loops and the combination of BufferedReader and InputStreamReader. These methods provide foundational skills for handling user input, an essential part of many Java applications.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.