int i = 1234;
char[] chars = ("" + i).toCharArray();
Answer from AlexR on Stack Overflowint i = 1234;
char[] chars = ("" + i).toCharArray();
You could try something like:
String.valueOf(1234).toCharArray();
Yeah, weโre missing a stream method to produce a char array. Maybe a whole CharStream class. In any case, no, you cannot cast between int[] and char[].
In the meantime, itโs getting a long line, but it works:
return IntStream.rangeClosed('a', 'z')
.mapToObj(c -> Character.toString((char) c))
.collect(Collectors.joining())
.toCharArray();
This gives a char[] containing
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
From Java-11 and onwards , you can use .mapToObj(Character::toString)
instead of .mapToObj(c -> Character.toString((char) c)) , so your overall code boils down to :
return IntStream.rangeClosed('a', 'z')
.mapToObj(Character::toString)
.collect(Collectors.joining())
.toCharArray();
Can we convert a char array to String in Java?
Can you assign a char to an int in Java?
How to take char as input after an integer in Java?
Hello, I need some help on how to convert an Int [][] to Char [][]. The values in Int table are numbers from 0 to 5 and I need to change their value to a certain character for each of them (?:/% _). Thank you for your time and help in advanced :)
Specifically, within Java. For example:
An array with (1, 0, 0, 0, 0, 1, 1) should return a char value of โCโ.
Thank you for your help.
Maybe you are asking for:
Character.toChars(65) // returns ['A']
More info: Character.toChars(int codePoint)
Converts the specified character (Unicode code point) to its UTF-16 representation stored in a char array. If the specified code point is a BMP (Basic Multilingual Plane or Plane 0) value, the resulting char array has the same value as codePoint. If the specified code point is a supplementary code point, the resulting char array has the corresponding surrogate pair.
It depends on what you mean by "convert an int to char".
If you simply want to cast the value in the int, you can cast it using Java's typecast notation:
int i = 97; // 97 is 'a' in ASCII
char c = (char) i; // c is now 'a'
If you mean transforming the integer 1 into the character '1', you can do it like this:
if (i >= 0 && i <= 9) {
char c = Character.forDigit(i, 10);
....
}
There are a number of issues with your solution. The first is the loop condition i > raw.length() is wrong - your loops is never executed - thecondition should be i < raw.length()
The second is the cast. You're attempting to cast to an integer array. In fact since the result is a char you don't have to cast to an int - a conversion will be done automatically. But the converted number isn't what you think it is. It's not the integer value you expect it to be but is in fact the ASCII value of the char. So you need to subtract the ASCII value of zero to get the integer value you're expecting.
The third is how you're trying to print the resultant integer array. You need to loop through each element of the array and print it out.
String raw = "1233983543587325318";
int[] num = new int[raw.length()];
for (int i = 0; i < raw.length(); i++){
num[i] = raw.charAt(i) - '0';
}
for (int i : num) {
System.out.println(i);
}
Two ways in Java 8:
String raw = "1233983543587325318";
final int[] ints1 = raw.chars()
.map(x -> x - '0')
.toArray();
System.out.println(Arrays.toString(ints1));
final int[] ints2 = Stream.of(raw.split(""))
.mapToInt(Integer::parseInt)
.toArray();
System.out.println(Arrays.toString(ints2));
The second solution is probably quite inefficient as it uses a regular expression and creates string instances for every digit.