Array

How to get array input and print in Java using while loop

How to get array input and print in Java using while loop

In this article, we will discuss the concept of How to get array input and print in Java using while loop

In this post, we are going to learn how to write a program to read array input and print elements in an array using while loop in java language

Code to read input and print of  array elements

Code to take input and print elements of an array using while loop

In this code, we are going to learn how to read integer array input given by user and print them using while loop in Java language

Program 1

//Java program to read and print elements a one dimensional array
import java.util.Scanner;
class Array_Sin_Dim_Int_while1{
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 user
int arr[]=new int[len]; //diclaration and creation

System.out.println("Enter the array elements");
//Ask input from user for array elements
int i=0;
while(i<len){
arr[i]=scan.nextInt(); 
//Reading the input
 i++;
}
System.out.println("\narray elements are:");
i=0; 
while(i<len){
System.out.println(arr[i]); 
 i++;
}//display every array element
}
}

When the above code is executed, it produces the following result

array input and print in java

 

Program 2

//Java program to read and print elements a one sin array using while
import java.util.Scanner;
class Array_Sin_Dim_Int_while2{
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: ");
int len=scan.nextInt();
int arr[]=new int[len]; //diclaration and creation

System.out.println("Enter the array elements");
//Ask enter the elemnts from user
int i=0;
while(i<len){
System.out.print("[arr["+i+"]:"); 
arr[i]=scan.nextInt(); 
//Reading the input for length
 i++;
}
System.out.println("\narray elements are:");
//Ask input from user
i=0;
while(i<len){
System.out.println("[arr["+i+"]:"+arr[i]); 
//Reading the input for elements
 i++;
}//display every array element
}
}

When the above code is executed, it produces the following result

array input and print in java

 

 

Code to take input and print string of an array using while loop

In this code, we are going to learn how to read  string  array input given by user and print the given input using while loop in Java language

Program 1

//Java program to read and print String an array using while
import java.util.Scanner;
class Array_Sin_Dim_Disp_String_While1{
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]; 
//diclaration and creation of an array

System.out.println("Enter the "+len+" String");
int i=0;
while(i<len){
arr[i]=scan.next(); 
//Taking the input from the user
 i++;
}
System.out.println("\nEntered string are:");

int j=0; 
while(j<len){
System.out.println(arr[j]); 
j++;
}//display every array element
 
}
}

When the above code is executed, it produces the following result

array input and print in java

 

Program 2

//Java program to read and print String a array using do-while
import java.util.Scanner;
class Array_Sin_Dim_Disp_String_While2{
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]; 
//diclaration and creation of an array

System.out.println("Enter the "+len+" String");
int i=0;
while(i<len){
  System.out.print("[arr["+i+"]:");
arr[i]=scan.next(); 
//Reding the input from the user
 i++;
}
System.out.println("\nEntered string are:");
int j=0; 
while(j<len){
System.out.println("[arr["+j+"]:"+arr[j]);
 j++;
}//display every array element
}
}

When the above code is executed, it produces the following result

array input and print in java

 

 

Code to take input and print character of an array using while loop

In this code, we are going to learn how to read character array input given by user and print the given input using while loop in Java language

Program 1

//Read and print character of an array using while loop
import java.util.Scanner;
class Arr_One_Dim_Char_While1{
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]; 
//diclaration and creation of char array

System.out.println("Enter the "+length+" Characters");
int i=0;
while(i<length){
arr[i]=sc.next().charAt(0); 
//Reding the input(characters) from the user
 i++;
}
System.out.println("\nEntered characters are:");
int j=0; 
while(j<length){
System.out.println(arr[j]); 
 j++;
}//display all entered characters
}
}

 

When the above code is executed, it produces the following result

Array input and print in java

 

Program 2

//Read and print character of an array using while loop
import java.util.Scanner;
class Arr_One_Dim_Char_While2{
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
char arr[]=new char[length]; 
//diclaration and creation of an array

System.out.println("Enter the "+length+" Characters");
int i=0;
while(i<length){
  System.out.print("[arr["+i+"]:");
arr[i]=sc.next().charAt(0); 
//Reding the input(characters) from the user
 i++;
}
System.out.println("\nEntered characters are:");
int j=0; 
while(j<length){
  System.out.print("[arr["+j+"]:");
System.out.println(arr[j]); 
 j++;
}//display all entered characters
}
}

When the above code is executed, it produces the following result

Array input and print in java

 

Suggested for you

For loop in Java language

Operator in Java language

Variable in Java language

Data type in Java language

class and main method in Java

One dimensional array in Java

 

Similar post

Java program to read and print array elements using for loop

Java program to read and print array elements using while loop

Java program to read and print array elements using Do-while loop

C code to read and print array elements using for loop

C code to read and print array elements using while loop

C code to read and print array elements using do-while loop

C++ program to read and print array elements using for loop

C++ program to read and print array elements using while loop

C++ program to read and print array elements using do-while loop

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.