Array

Java program to fill an array of characters from user input

Java program to fill an array of characters from user input

In this tutorial, we will discuss the concept of Java program to fill an array of characters from user input

In this topic, we are going to learn how to input character array elements 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)

Char array

Here,

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

Input Character of an array  in Java

Program to fill array of Characters  using for loop

In this program, we are briefing how to input character elements of an array using for loop in Java language

Program 1

import java.util.Scanner;
public class TakeCharArrayInputfor{
public static void main(String args[]){
//scanner class to read input from the user
    Scanner sc=new Scanner(System.in);
    //Declaring and creating character array
    char[] arr=new char[5];
    
    //initializing value to the array
    System.out.println("******Initializing array******");
    System.out.println("Enter character values");
    
    arr=sc.next().toCharArray();
        //Take character input 

    
    //displaying the array elements
    System.out.println("\n******displaying array of characters******");
    System.out.println("Entered Characters are");
    for(int i=0; i<arr.length; i++){
        System.out.print(arr[i]+"\t");
    }
}
}

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

******Initializing array******
Enter character values
ASDFGH

******displaying array of characters******
Entered characters are
A     S     D     F     G     H

 

Program to fill array of characters  using while loop

In this program, we are briefing how to input character elements of an array using while loop in Java language

Program 2

import java.util.Scanner;
public class TakeCharArrayInputwhile{
public static void main(String args[]){
//scanner class to read input from the user
    Scanner sc=new Scanner(System.in);
    //Declaring and creating character array
    char[] arr=new char[5];
    
    //initializing value to the array
    System.out.println("******Initializing array******");
    System.out.println("Enter character values");
    
    arr=sc.next().toCharArray();
        //Take character input 

    
    //displaying the array elements
    System.out.println("\n******displaying array of characters******");
    System.out.println("Entered Characters are");
    int i=0;
    while(i<arr.length){
        System.out.print(arr[i]+"\t");
         i++;
    }
}
}

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

******Initializing array******
Enter character values
Character

******displaying array of characters******
Entered characters are
C    h    a    r    e    c    t    e    r


 

Program to fill array of characters using do-while loop

In this program, we are briefing how to input character elements of an array using do-while loop in Java language

Program 3

import java.util.Scanner;
public class TakeCharArrayInputdowhile{
public static void main(String args[]){
//scanner class to read input from the user
    Scanner sc=new Scanner(System.in);
    //Declaring and creating character array
    char[] arr=new char[5];
    
    //initializing value to the array
    System.out.println("******Initializing array******");
    System.out.println("Enter character values");
    
    arr=sc.next().toCharArray();
        //Take character input 

    
    //displaying the array elements
    System.out.println("\n******displaying array of characters******");
    System.out.println("Entered Characters are");
    int i=0;
    do{
        System.out.print(arr[i]+"\t");
         i++;
    }while(i<arr.length);
}
}

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

******Initializing array******
Enter character values
Code4coding

******displaying array of characters******
Entered characters are
C    o    d    e     4    c    o    d    i    n    g


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

 

 

Take String Array input in Java language
Program to print Characters of a string 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# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

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

1 month 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…

1 month 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…

1 month ago

C Program to multiplication table using Array

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

1 month 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…

1 month ago