input char arrayjava

https://‮‬www.lautturi.com
input char arrayjava

To input a character array in Java, you can use the Scanner class or the BufferedReader class.

Here is an example of how to use the Scanner class to input a character array:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = sc.nextLine();
        char[] array = str.toCharArray();
        for (char c : array) {
            System.out.print(c + " ");
        }
        sc.close();
    }
}

In this example, the Scanner class is used to read a line of input from the console. The nextLine() method is used to read the line as a String, and the toCharArray() method is used to convert the String to a character array. The elements of the array are then printed to the console using a for-each loop.

Here is an example of how to use the BufferedReader class to input a character array:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter a string: ");
        String str = br.readLine();
        char[] array = str.toCharArray();
        for (char c : array) {
            System.out.print(c + " ");
        }
        br.close();
    }
}
Created Time:2017-11-01 22:29:52  Author:lautturi