How to read input and print elements of an array in Java using for loop
Posted on
Table of Contents
How to read input and print elements of an array in Java using for loop
In this article, we will discuss the concept of How to read input and print elements of an array in Java using for loop
In this post, we are going to learn how to write a program to read array input and print them in an array using for loop in Java language
Code to input and print of array elements
Code to take input and print elements of an array using for loop
In this code, we are going to learn how to read integer array input given by user and print them using for loop in Java language
Program 1
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Java program to read and print elements a array using for
import java.util.Scanner;
class Array_Sin_Dim_Int_For1{
public static voidmain(String args[]){
Scanner scan=newScanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
int arr[]=new int[len];
//declaration and creation of a array
System.out.println("Enter the array elements");
//Ask input for array elements
for(int i=0; i<len; i++){
arr[i]=scan.nextInt();
//Reding the input from the user
}
System.out.println("\narray elements are:");
for(int i=0; i<len; i++){
System.out.println(arr[i]);
}//display every array element
}
}
//Java program to read and print elements a array using for
import java.util.Scanner;
class Array_Sin_Dim_Int_For1{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
int arr[]=new int[len];
//declaration and creation of a array
System.out.println("Enter the array elements");
//Ask input for array elements
for(int i=0; i<len; i++){
arr[i]=scan.nextInt();
//Reding the input from the user
}
System.out.println("\narray elements are:");
for(int i=0; i<len; i++){
System.out.println(arr[i]);
}//display every array element
}
}
//Java program to read and print elements a array using for
import java.util.Scanner;
class Array_Sin_Dim_Int_For1{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
int arr[]=new int[len];
//declaration and creation of a array
System.out.println("Enter the array elements");
//Ask input for array elements
for(int i=0; i<len; i++){
arr[i]=scan.nextInt();
//Reding the input from the user
}
System.out.println("\narray elements are:");
for(int i=0; i<len; i++){
System.out.println(arr[i]);
}//display every array element
}
}
When the above code is executed, it produces the following result
input and print elements of an array
Program 2
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Java program to read and print elements a one dim array using for
import java.util.Scanner;
class Array_Sin_Dim_Int_For2{
public static voidmain(String args[]){
Scanner scan=newScanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask to enter length of array
int len=scan.nextInt();//Reading the input for length
int arr[]=new int[len]; //declaration and creation
System.out.println("Enter the array elements");
//Ask to enter the elements
for(int i=0; i<len; i++){
System.out.print("[arr["+i+"]:");
arr[i]=scan.nextInt(); //Store the elements using loop
}
System.out.println("\narray elements are:");
for(int i=0; i<len; i++){
System.out.println("[arr["+i+"]:"+arr[i]);
}//display every array element
}
}
//Java program to read and print elements a one dim array using for
import java.util.Scanner;
class Array_Sin_Dim_Int_For2{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask to enter length of array
int len=scan.nextInt();//Reading the input for length
int arr[]=new int[len]; //declaration and creation
System.out.println("Enter the array elements");
//Ask to enter the elements
for(int i=0; i<len; i++){
System.out.print("[arr["+i+"]:");
arr[i]=scan.nextInt(); //Store the elements using loop
}
System.out.println("\narray elements are:");
for(int i=0; i<len; i++){
System.out.println("[arr["+i+"]:"+arr[i]);
}//display every array element
}
}
//Java program to read and print elements a one dim array using for
import java.util.Scanner;
class Array_Sin_Dim_Int_For2{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask to enter length of array
int len=scan.nextInt();//Reading the input for length
int arr[]=new int[len]; //declaration and creation
System.out.println("Enter the array elements");
//Ask to enter the elements
for(int i=0; i<len; i++){
System.out.print("[arr["+i+"]:");
arr[i]=scan.nextInt(); //Store the elements using loop
}
System.out.println("\narray elements are:");
for(int i=0; i<len; i++){
System.out.println("[arr["+i+"]:"+arr[i]);
}//display every array element
}
}
When the above code is executed, it produces the following result
input and print elements of an array
Code to take input and print strings of an array using for loop
In this code, we are going to learn how to read string array input given by user and print them using for loop in Java language
Program 1
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Java program to read and print string of an array using for
import java.util.Scanner;
class Arr_Sin_Dim_Disp_String_For1{
public static voidmain(String args[]){
Scanner sc=newScanner(System.in);
//create a scanner object for Taking input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int length=sc.nextInt();
//Reading the input from the user
String arr[]=new String[length];
//declaration and creation of an array
System.out.println("Enter the "+length+" String");
for(int i=0; i<length; i++){
arr[i]=sc.next();
//Reding the input(String) from the user
}
System.out.println("\nEntered strings are:");
for(int j=0; j<length; j++){
System.out.println(arr[j]);
}//display all entered string
}
}
//Java program to read and print string of an array using for
import java.util.Scanner;
class Arr_Sin_Dim_Disp_String_For1{
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
//create a scanner object for Taking input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int length=sc.nextInt();
//Reading the input from the user
String arr[]=new String[length];
//declaration and creation of an array
System.out.println("Enter the "+length+" String");
for(int i=0; i<length; i++){
arr[i]=sc.next();
//Reding the input(String) from the user
}
System.out.println("\nEntered strings are:");
for(int j=0; j<length; j++){
System.out.println(arr[j]);
}//display all entered string
}
}
//Java program to read and print string of an array using for
import java.util.Scanner;
class Arr_Sin_Dim_Disp_String_For1{
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
//create a scanner object for Taking input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int length=sc.nextInt();
//Reading the input from the user
String arr[]=new String[length];
//declaration and creation of an array
System.out.println("Enter the "+length+" String");
for(int i=0; i<length; i++){
arr[i]=sc.next();
//Reding the input(String) from the user
}
System.out.println("\nEntered strings are:");
for(int j=0; j<length; j++){
System.out.println(arr[j]);
}//display all entered string
}
}
When the above code is executed, it produces the following result
input and print elements of an array
Program 2
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Java program to read and print elements a array using for
import java.util.Scanner;
class Arr_Sin_Dim_Disp_String_For2{
public static voidmain(String args[]){
Scanner scan=newScanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
String arr[]=new String[len];
//declaration and creation of an array
System.out.println("Enter the "+len+" String");
for(int i=0; i<len; i++){
System.out.print("[arr["+i+"]:");
arr[i]=scan.next();
//Reding the input from the user
}
System.out.println("\nEntered string are:");
for(int j=0; j<len; j++){
System.out.println("[arr["+j+"]:"+arr[j]);
}//display every array element
}
}
//Java program to read and print elements a array using for
import java.util.Scanner;
class Arr_Sin_Dim_Disp_String_For2{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
String arr[]=new String[len];
//declaration and creation of an array
System.out.println("Enter the "+len+" String");
for(int i=0; i<len; i++){
System.out.print("[arr["+i+"]:");
arr[i]=scan.next();
//Reding the input from the user
}
System.out.println("\nEntered string are:");
for(int j=0; j<len; j++){
System.out.println("[arr["+j+"]:"+arr[j]);
}//display every array element
}
}
//Java program to read and print elements a array using for
import java.util.Scanner;
class Arr_Sin_Dim_Disp_String_For2{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
String arr[]=new String[len];
//declaration and creation of an array
System.out.println("Enter the "+len+" String");
for(int i=0; i<len; i++){
System.out.print("[arr["+i+"]:");
arr[i]=scan.next();
//Reding the input from the user
}
System.out.println("\nEntered string are:");
for(int j=0; j<len; j++){
System.out.println("[arr["+j+"]:"+arr[j]);
}//display every array element
}
}
When the above code is executed, it produces the following result
input and print elements of an array
Code to take input and print character of an array using for loop
In this code, we are going to learn how to read characters array input given by user and print them using for loop in Java language
Program 1
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Read and print character of an array using for looop
import java.util.Scanner;
class Arr_One_Dim_Char_For1{
public static voidmain(String args[]){
Scanner sc=newScanner(System.in);
//create a scanner instance for Taking input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int length=sc.nextInt();
//Reading the input from the user for length
char arr[]=new char[length];
//declaration and creation of character array
System.out.println("Enter the "+length+" Characters");
for(int i=0; i<length; i++){
arr[i]=sc.next().charAt(0);
//Reding the input(characters) from the user
}
System.out.println("\nEntered characters are:");
for(int j=0; j<length; j++){
System.out.println(arr[j]);
}//display all entered characters
}
}
//Read and print character of an array using for looop
import java.util.Scanner;
class Arr_One_Dim_Char_For1{
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
//create a scanner instance for Taking input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int length=sc.nextInt();
//Reading the input from the user for length
char arr[]=new char[length];
//declaration and creation of character array
System.out.println("Enter the "+length+" Characters");
for(int i=0; i<length; i++){
arr[i]=sc.next().charAt(0);
//Reding the input(characters) from the user
}
System.out.println("\nEntered characters are:");
for(int j=0; j<length; j++){
System.out.println(arr[j]);
}//display all entered characters
}
}
//Read and print character of an array using for looop
import java.util.Scanner;
class Arr_One_Dim_Char_For1{
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
//create a scanner instance for Taking input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int length=sc.nextInt();
//Reading the input from the user for length
char arr[]=new char[length];
//declaration and creation of character array
System.out.println("Enter the "+length+" Characters");
for(int i=0; i<length; i++){
arr[i]=sc.next().charAt(0);
//Reding the input(characters) from the user
}
System.out.println("\nEntered characters are:");
for(int j=0; j<length; j++){
System.out.println(arr[j]);
}//display all entered characters
}
}
When the above code is executed, it produces the following result
input and print elements of an array
Program 2
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Read and print character of an array using while loop
import java.util.Scanner;
class Arr_One_Dim_Char_For2{
public static voidmain(String args[]){
Scanner sc=newScanner(System.in);
//create a scanner object for Taking input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int length=sc.nextInt();
//Reading the input from the user for array length
char arr[]=new char[length];
//declaration and creation of a char array
System.out.println("Enter the "+length+" Characters");
for(int i=0; i<length; i++){
System.out.print("[arr["+i+"]:");
arr[i]=sc.next().charAt(0);
//Reding the input(characters) from the user
}
System.out.println("\nEntered characters are:");
for(int j=0; j<length; j++){
System.out.print("[arr["+j+"]:");
System.out.println(arr[j]);
}//display all entered characters
}
}
//Read and print character of an array using while loop
import java.util.Scanner;
class Arr_One_Dim_Char_For2{
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
//create a scanner object for Taking input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int length=sc.nextInt();
//Reading the input from the user for array length
char arr[]=new char[length];
//declaration and creation of a char array
System.out.println("Enter the "+length+" Characters");
for(int i=0; i<length; i++){
System.out.print("[arr["+i+"]:");
arr[i]=sc.next().charAt(0);
//Reding the input(characters) from the user
}
System.out.println("\nEntered characters are:");
for(int j=0; j<length; j++){
System.out.print("[arr["+j+"]:");
System.out.println(arr[j]);
}//display all entered characters
}
}
//Read and print character of an array using while loop
import java.util.Scanner;
class Arr_One_Dim_Char_For2{
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
//create a scanner object for Taking input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int length=sc.nextInt();
//Reading the input from the user for array length
char arr[]=new char[length];
//declaration and creation of a char array
System.out.println("Enter the "+length+" Characters");
for(int i=0; i<length; i++){
System.out.print("[arr["+i+"]:");
arr[i]=sc.next().charAt(0);
//Reding the input(characters) from the user
}
System.out.println("\nEntered characters are:");
for(int j=0; j<length; j++){
System.out.print("[arr["+j+"]:");
System.out.println(arr[j]);
}//display all entered characters
}
}
When the above code is executed, it produces the following result