Table of Contents
In this article, we will discuss the concept of Java program to print an array of characters
In this post, we are going to learn how to write a program to print array of characters (character elements in single dimensional array) using for, while and do-while loop in Java language
In this code, we are going to learn how to print character elements (user input) in single dimensional array using for loop in Java language
Program 1
//Print character of an array using for looop import java.util.Scanner; class Disp_Array_Char_ForA{ public static void main (String args[]){ Scanner sc=new Scanner(System.in); //Scanner class for user input System.out.println("Enter array length: "); int len=sc.nextInt(); char[] charArr=new char[len]; //Declaring a character Array System.out.println("Enter the "+len+" Characters"); for(int i=0; i<len; i++){ charArr[i]=sc.next().charAt(0); //Reding the input(characters) from the user } System.out.println("\n*** Print Characters ***"); System.out.println("Entered characters are:"); for(int j=0; j<len; j++){ System.out.println(charArr[j]); }//display all entered characters } }
When the above code is executed, it produces the following result
Enter Array length 8 Enter the 8 characters C o d e G e e k *** Print Characters *** Entered characters are: C o d e G e e k
In this code, we are going to learn how to print character elements (user input) in single dimensional array using while loop in Java language
Program 2
//Print character of an array using while looop import java.util.Scanner; class Disp_Array_Char_WhileA{ public static void main (String args[]){ Scanner sc=new Scanner(System.in); //Scanner class for user input System.out.println("Enter array length: "); int len=sc.nextInt(); char[] charArr=new char[len]; //Declaring a character Array System.out.println("Enter the "+len+" Characters"); int i=0; while(i<len){ charArr[i]=sc.next().charAt(0); //Reding the input(characters) from the user i++; } System.out.println("\n*** Print Characters ***"); System.out.println("Entered characters are:"); int j=0; while(j<len){ System.out.println(charArr[j]); j++; }//display all entered characters } }
When the above code is executed, it produces the following result
Enter Array length 8 Enter the 8 characters C o d e 4 Y o u *** Print Characters *** Entered characters are: C o d e 4 Y o u
In this code, we are going to learn how to print character elements (user input) in single dimensional array using do-while loop in Java language
Program 3
//Print character of an array using do-while looop import java.util.Scanner; class Disp_Array_Char_DoWhileA{ public static void main (String args[]){ Scanner sc=new Scanner(System.in); //Scanner class for user input System.out.println("Enter array length: "); int len=sc.nextInt(); char[] charArr=new char[len]; //Declaring a character Array System.out.println("Enter the "+len+" Characters"); int i=0; do{ charArr[i]=sc.next().charAt(0); //Reding the input(characters) from the user i++; }while(i<len); System.out.println("\n*** Print Characters ***"); System.out.println("Entered characters are:"); int j=0; do{ System.out.println(charArr[j]); j++; }while(j<len); //display all entered characters } }
When the above code is executed, it produces the following result
Enter Array length 7 Enter the 7 characters P r o g r a m *** Print Characters *** Entered characters are: P r o g r a m
Suggested for you
One dimensional array in C language
One dimensional array in C++ language
Do-while loop in Java language
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 take and print array elements using for loop
C code to take and print array elements using while loop
C code to take 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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.