Char Display And Whole Together

My code is in Java. I have an array char

that has values int

as well as values char

. When I type, I get its ASCII related values. But when I convert the output to int

, my character value changes to its ASCII value.

char arr[]=new char [5];
for(int i=0;i<5;i++)
{
    arr[i]=(char)(i+1);
}

arr[0]='@';

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

      

Or converting to int

:

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

      

My conclusion

64 2 3 4 5

or

@

depending on what code i am using. However, I want the result to be

@ 2 3 4 5

I tried to use method overloading to separate integers and characters, but it didn't work.

Is there a condition for int

as below?

if(arr[i]==int)

      

The code doesn't work, but is there something similar to it or any other solution?

+3


source to share


2 answers


This doesn't work because, as far as Java is concerned, a primitive char

is just a 16-bit unsigned integer. To demonstrate:

System.out.println('A' == 65); // prints true

      

One way to get around this is to declare an array of objects and rely on auto-boxing to have Java automatically convert your primitive value to the wrapper object, Integer

or Character

:

Object arr[] = new Object[2];
arr[0] = '@'; // stores '@' as a Character wrapper object
arr[1] = 1; // stores 1 as an Integer wrapper object

for (Object o : arr) {
    System.out.println(o);
}

      



Unlike primitives, wrapper objects know about their type, so this prints:

@ // by calling Character.toString()
1 // by calling Integer.toString()

      

Note that you lose some compile time checking in the process. Your array of objects will not only accept values Character

and Integer

, but any other value as well.

+3


source


Robbie is very correct! However, if you're just looking for a hack to quickly convert between ints and chars, you can take advantage of ASCII values ​​and use offset (48) in this case, which is the value for char '0'

, and then use that to convert between primitive types. So I used an array to store the resulting integers for output purposes.

    public class Hack{
        public static void main(String []args){
                char arr[]=new char [5];
                int offset = 48;

                for(int i=0;i<5;i++)
                {
                        arr[i]=(char)(i+1 + offset);
                }

                //arr[0]='@';

                for(int i=0;i<5;i++)
                {
                        System.out.println(arr[i]);
                }
                int num_arr[] = new int [5];

                for(int i=0;i<5;i++)
                {
                        num_arr[i]= ( arr[i] - offset);
                }
                System.out.println("\n");

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

      



Output:

1
2
3
4
5


1
2
3
4
5

      

0


source







All Articles