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)
Here,
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
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
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
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
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
Do-while loop in Java language
Enhanced for loop in Java language
10 simple ways to add two numbers in Java In this article, we will discuss…
Write a Python program to find the first n prime numbers In this article we…
Python: Calculate Average of odd and even in a list using loops In this post,…
Python: Average of Odd & Even Numbers from User Input In this post, we will…
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…