Java Collection How to - Fill an array of characters from user input








Question

We would like to know how to fill an array of characters from user input.

Answer

import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStreamReader;
//w ww .  j a  va2  s . c  o  m
public class Main {

  public static void main(String args[]) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String tmp = br.readLine();
    int length = tmp.length();
    char c[] = new char[length];
    tmp.getChars(0, length, c, 0);
    CharArrayReader input1 = new CharArrayReader(c);
    int i;
    System.out.print("input1 is:");
    while ((i = input1.read()) != -1) {
      System.out.print((char) i);
    }

  }
}

The code above generates the following result.