2

Is there an easy way in Java to convert space separated values to char array.

Example: "a b c" must become ['a','b','c'];

I tried

Scanner scanner = new Scanner(System.in);
char[] letters = Arrays.stream(scanner.nextLine().split(" "))
                       .map(a->a.charAt(0)).toArray();                 

, but this returns an Object[] instead of char[], so it doesn't work.

I know I can do it this way:

    Scanner scanner = new Scanner(System.in);
    ArrayList<Character> letters = new ArrayList<>();
    char[] line = scanner.nextLine().toCharArray();
    for (int i = 0; i < line.length; i++) {
          if(line[i]!=' '){
              letters.add(line[i]);
          }
    }

But I was wondering is there a more direct approach?

7 Answers 7

2

Sure:

char[] letters = scanner.nextLine().replace(" ", "").toCharArray(); 

See KISS Principle.

1

The main problem is that streams are not thought to handle primitives type like char or byte. The only primitives type that you can handle by default are int, long and double. So, if you want use streams and lambdas you have to switch to the Object version for each type which is in your case the Character object.

toArray() method returns an array of object, you have to use .collect(Collectors.toList()) which returns a ArrayList.

List<Character> collect = Arrays.stream(scanner.nextLine().split(" "))
                       .map(a -> new a.charAt(0)).collect(Collectors.toList());
2
  • Thanks for the explanation, it is very helpful :)
    – Yangra
    Apr 4, 2017 at 9:06
  • :) Thanks. The handling of primitive types with streams creates few subtle difference compared with the classic flow that sometimes make the life hard
    – freedev
    Apr 4, 2017 at 9:08
1

Why simply using your solution with a simple extension to remove the whitespaces?:

char[] letters = scanner.nextLine().replaceAll("\s","").toCharArray();
0

Elaborating from your code using a stream, you could use a Collector to build a List:

ArrayList<Character> letters = Arrays.stream(scanner.nextLine().split(" "))
                   .map(a->a.charAt(0)).collect(Collectors.toList());

Also, in case you are using or willing to use the Guava utility library, you could find Splitter useful in place of the split method (although it does not directly solve your specific problem).

1
  • They want an array not a list (although I don't know why)
    – slim
    Apr 4, 2017 at 8:58
0

If you can live with an Array of Character[] type, you can use this method:

Character[] letters = Arrays.stream(scanner.nextLine().split(" ")).map(a -> a.charAt(0)).toArray(Character[]::new);

Unfortunately, this does not work with char[]::new, so if you need to have the primitive char[] type, you have to convert the array to char[] afterwards. You can either use Apache Commons Lang's method

ArrayUtils.toPrimitive(letters);

or do this yourself

char[] a = new char[letters.length];
for (int i = 0; i < letters.length; i++) {
    a[i] = letters[i];
}
0

This should work:

char[] letters = Arrays.stream(scanner.nextLine().split("\\s+"))
                       .collect(Collectors.joining(""))
                       .toCharArray();

But I'd probably use a Guava CharMatcher:

char[] letters = CharMatcher.WHITESPACE.removeFrom(scanner.nextLine())
                                       .toCharArray();
0

Your key issue here is that Stream is inherently a stream of objects, not primitives.

There is are parallel types IntStream, LongStream, DoubleStream but there is no CharStream.

You might decide that using int instead of char is enough for you, and you could do that with:

 int[] letters = Arrays.stream(scanner.nextLine().split(" "))
                   .mapToInt( a-> (int) a.charAt(0))
                   .toArray();

Note that this is good enough for the designers of the Java library: String.chars() returns an IntStream.

Alternatively you could:

  • write your own Collector
  • or convert the int[] to char[] as a separate step
  • or just use String.toCharArray()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.