Array

Read and print Characters of one dim array in Java language

Read and print Characters of one dim array in Java language

In this tutorial, we will discuss the concept of Read and print Characters of one dim array in Java language

In this topic, we are going to learn how to read and print Characters using  one dimensional array in Java programming language  using loops.

 

In this blog, We have already discuss that “What is an array“, type of arrays and how to access it(one dim, two dim and three dim arrays)

Arrays are the special type of variables to store Multiple type of values(int, string,char, etc)under the same name in the continuous memory location. we can be easily accessed using the indices(indexes)

Read and print Characters of array

Here,

  • C,H,A,R represent the character elements of the array
  • 0,1,2,3 and 4 represent the index of the array which is used to access the array elements
  • 1,2,3,4 and 5 represent the length of the array

Code to input and display characters of one dim array

Code to input and display – Using for loop

In this program, we are briefing input characters of an array and then print them using for loop in Java language

Program 1

//Java program to read and print character of an array using for
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 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");
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

Enter the array length:6

Enter the 6 characters
v
a
n
n
a
n

 

Entered characters are

v
a
n
n
a
n


Program 2

//Java program to read and print character of an array using for
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
char arr[]=new char[length]; 
//diclaration and creation of an 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

Read and print Characters of array

 

Code to input and display – Using while loop

In this program, we are briefing input characters of an array and then print them using while loop in Java language

Program 1

//Java program to read and print character of an array using while
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

Enter the array length:7

Enter the 7 characters
P
r
o
g
r
a
m




Entered characters are

P
r
o
g
r
a
m

 

Program 2

//Java program to read and print character of an array using while
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

Read and print Characters of array

 

 

Code to input and display -Using do-while loop

In this program, we are briefing input characters of an array and then print them using do-while loop in Java language

Program 1

//Java program to read and print character of an array using do-while
import java.util.Scanner;
class Arr_One_Dim_Char_DoWhile1{
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;
do{
arr[i]=sc.next().charAt(0); 
//Reding the input(characters) from the user
 i++;
}while(i<length);
System.out.println("\nEntered characters are:");
int j=0; 
do{
System.out.println(arr[j]); 
 j++;
}while(j<length);
//display all entered characters
}
}

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

Enter the array length:4
Enter the 4 characters
C
h
a
r

Entered characters are
C
h
a
r

 

Program 2

//Java program to read and print character of an array using do-while
import java.util.Scanner;
class Arr_One_Dim_Char_DoWhile2{
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; 
do{
    System.out.print("[arr["+i+"]:");
arr[i]=sc.next().charAt(0); 
//Reding the input(characters) from the user
 i++;
}while(i<length);
System.out.println("\nEntered characters are:");
int j=0; 
do{
    System.out.print("[arr["+j+"]:");
System.out.println(arr[j]); 
 j++;
}while(j<length);
//display all entered characters
}
}

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

Read and print Characters of array

 

 

Similar post

Code to read and print integers of an array in Java

Code to read and print strings of an array in Java

Code to read and print characters of an array in Java

Code to read and print integers of an array in C++

Code to read and print strings of an array in C++

Code to read and print characters of an array in C++

Code to read and print integers of an array in C

Code to read and print strings of an array in C

 

Suggested for you

Array in Programming language

One dimensional array in C language

One dimensional array in C++ language

Two dimension array in Java language

Two dimension array in C language

Two dimension array in C++ language

Three dimension array in Java language

Three dimension array in C language

Three dimension array in C++ language

 

 

for loop in Java language

while loop in Java language

Do-while loop in Java language

Enhanced for loop in Java language

Data  type and variable in Java language

Operator in Java language

C++ program to read and display characters of an array
Program to print array elements in Java
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 weeks ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

2 weeks ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

2 weeks ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

4 weeks ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

4 weeks ago

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

7 months ago