-3

I couldn't assign int variables into char[] array

I tried type casting, but it didn't work as I expected
the complier keeps return me ASCII value which I didn't want them. I want the numbers the actual number

char[] char_array = new char[10];
for(int i = 0; i < 10; i ++)
{
  int calculation = i * (d - 1);
  char_array[i] = (char) calculation;
}

After print out
It gives me something like this

| B @ #

user3505908
  • 5
  • 1
  • 7
  • `i *8 /2 ...;` isn't a valid Java expression – Andrew Tobilko Sep 09 '19 at 11:05
  • `System.out.print((int)'a');` – diginoise Sep 09 '19 at 11:08
  • If you want the numbers, store them into an `int[]`. You're using `char[]`, which contains characters, and by default they're printed as characters instead of their numeric value. – Kayaman Sep 09 '19 at 11:08
  • Using System.out.print(calculation) returns aal the numbers just fine, but the problem is those values coudln't be converted into char type. Are there any ways to fix this? Thhank you. – user3505908 Sep 09 '19 at 11:09
  • No, i *8 /2 ...; is just the example. it was actually d * (d - 1) – user3505908 Sep 09 '19 at 11:10
  • Maybe I should delete this question – user3505908 Sep 09 '19 at 11:13
  • What do you mean by "something like this `| B @ #`"? Is it something like `[B@ab47d2`? If so take a look at [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784) – Pshemo Sep 09 '19 at 11:53
  • If that is not the case then please provide [mcve] (a.k.a. [SSCCE](http://sscce.org)) and *actual* results you are getting. Also include what *other* result you ware expecting (and why). Without it we need to play guessing game which is not very efficient way of solving problems. – Pshemo Sep 09 '19 at 11:59

3 Answers3

2

You can't use char to store numeric values. Its because when your store an int value to a char variable type conversion occurs as follows:

char i = 60 + 5;

Here, i will store A which has Unicode value 65.

Similarly, when you store a char value to an int, type conversion occurs as follows:

int i = 'A'

This will store 65;

This is how java performs type cast between int and char;

So, to deal with this situation, use the following code:

int[] int_array = new int[10];
for(int i = 0; i < 10; i ++)
{
int calculation = i *8 /2 ...;
int_array[i] = calculation;
}

or you can also use double if you want precise results;

  • Java doesn't use ASCII. `char` is a UTF-16 code unit. UTF-16 is one of several character encodings for the Unicode character set. – Tom Blodget Sep 09 '19 at 22:33
0

try, this should help you.

int a = 1
char b = Integer.toString(a).charAt(0);
System.out.println(b);
Jayant Varshney
  • 1,765
  • 1
  • 25
  • 42
0

For printing number, you should cast the output to int.

char[] char_array = new char[10];
for(int i = 0; i < 10; i ++)
{
  int calculation = i * 4;
  char_array[i] = (char) calculation;
}

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

But in this case consider to use a int[] directly instead casting forth and back.

Also note, that overflow can happen when storing char c = (int) 256.

Sparkofska
  • 1,280
  • 2
  • 11
  • 34