Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Java Program to fill an array of characters from user input



For user input, use the Scanner class with System.in. After getting the input, convert it to character array −

char[] a = s.next().toCharArray();

Now, display it until the length of the character array i.e. number of elements input by the user −

for (int i = 0; i < a.length; i++) {
   System.out.println(a[i]);
}

To fill an array of characters from user input, use Scanner class.

Example

import java.util.Scanner;
public class Demo {
   public static void main(String args[]) {
      Scanner s = new Scanner(System.in);
      System.out.println("First add some characters...");
      char[] a = s.next().toCharArray();
      System.out.println("Elements = ");
      for (int i = 0; i < a.length; i++) 
         System.out.println(a[i]);
   }
}

Now, enter some input −

RSTUVWXYZ

After entering, you need to run the program to get the following output −

Output

First add some characters...
Elements entered =
R
S
T
U
V
W
X
Y
Z
Updated on: 2024-06-18T16:45:18+05:30

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements