BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to take array input in Java

Last Updated: June 1, 2024 by Chaitanya Singh | Filed Under: java

In this guide, you will learn how to take 1D array and 2D array input in Java. We will be using for loop and Scanner class to accomplish this.

Java Program to take 1D array Input

import java.util.Scanner;

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

// Prompt user to enter the array size
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();

//Declare array based on entered size
int[] array = new int[size];

System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

// Print the array
System.out.println("Array elements:");
for (int num : array) {
System.out.print(num + " ");
}

scanner.close();
}
}

Output:

Enter the size of the array: 5
Enter the elements of the array:
10
20
30
40
50
Array elements:
10 20 30 40 50

Points to Note:

  • You need to import the java.util.Scanner package to use the methods of Scanner class.
  • Always remember to use scanner.close() to release resources after you finish reading user input.

Java Program to take 2D array Input

The approach is similar here, however in order to take 2D array inputs, we need to use the nested for loop. Also, we need additional row and column inputs from user.

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();
}
}

Output:

Enter the number of rows: 2
Enter the number of columns: 3
Enter the elements of the array:
10
20
30
40
50
60
Array elements:
10 20 30
40 50 60

Points to Note:

  • You must ensure that the input values are either separated by spaces or newlines.
  • In the above programs, we are using integer arrays, however you can modify the program to handle other types of arrays such as double or String arrays. This can be simply done by changing the data type of the array and replacing the nextInt() method to the appropriate methods (e.g., nextDouble() or nextLine()).

Top Related Articles:

  1. When to use ArrayList and LinkedList in Java
  2. Remove special characters from a String in Java
  3. How to Compile and Run your First Java Program
  4. Java Scanner class with examples
  5. Convert Comma Separated String to HashSet in Java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap