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
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Java program to read and print character of an array using for
import java.util.Scanner;
class Arr_One_Dim_Char_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
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
}
}
//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
}
}
//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
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Java program to read and print character of an array using for
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
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
}
}
//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
}
}
//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
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Java program to read and print character of an array using while
import java.util.Scanner;
class Arr_One_Dim_Char_While1{
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];
//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
}
}
//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
}
}
//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
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Java program to read and print character of an array using while
import java.util.Scanner;
class Arr_One_Dim_Char_While2{
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
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
}
}
//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
}
}
//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
//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 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];
//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
}
}
//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
}
}
//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
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//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 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
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
}
}
//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
}
}
//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