Integer and Unicode values for characters? - java

Here is some code:
private static final char low = 'a';
private static final char high = 'z';
private static final int arrayLength = high - low + 1;
I think arrayLength will be equal to 26, but I'm not sure. Is this correct? My second question is that what is the numerical value difference between 'a' and 'A'? When I used the getNumericValue() method in the Character class to test both characters:
String element = "a";
int x = Character.getNumericValue(element.charAt(0));
I tested both 'a' and 'A' and I got 10 both times. So, in general, I'm confused about the numerical values of chars. Please advise.

Character.getNumericValue() is for getting the value of numeric digits or symbols - so, for example, Character.getNumericValue('7') would return 7. As a is a digit in hexadecimal, this method returns the hexadecimal value of a, or 10.
For the Unicode value of a, do (int) 'a' or (int) "a".charAt(0). **
(** This doesn't work for Unicode characters in astral planes, but these are rare.)

You did not bother to read the javaDoc for getNumericalValue(), so I'll add the link, and tell you that the numerical value for both A and a are supposed to be 10.

You could also you test it m8
public class Main {
private static final char low = 'a';
private static final char high = 'z';
private static final int arrayLength = high - low + 1;
public static void main(String [ ] args) {
System.out.println("a: "+Character.getNumericValue(low));
System.out.println("z: "+Character.getNumericValue(high));
System.out.println("The arrayLenght is: "+arrayLength);
}
}
Console output:
a: 10
z: 35
The arrayLenght is: 26

Character.getNumericValue(char);
work like this
char 0 to 9 return int 0 to 9
char a (or) A to z (or) Z return 10 to 35.
that mean Alphanumeric.
and unicode also include.
other char value return -1.

Related

The conversion of int to char is not printing anything [duplicate]

Below is a code snippet,
int a = 1;
char b = (char) a;
System.out.println(b);
But what I get is empty output.
int a = '1';
char b = (char) a;
System.out.println(b);
I will get 1 as my output.
Can somebody explain this? And if I want to convert an int to a char as in the first snippet, what should I do?
int a = 1;
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 1 (start-of-heading char, which isn't printable; see this table: C0 Controls and Basic Latin, same as ASCII)
int a = '1';
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 49 (one corresponding to '1')
If you want to convert a digit (0-9), you can add 48 to it and cast, or something like Character.forDigit(a, 10);.
If you want to convert an int seen as a Unicode code point, you can use Character.toChars(48) for example.
My answer is similar to jh314's answer but I'll explain a little deeper.
What you should do in this case is:
int a = 1;
char b = (char)(a + '0');
System.out.println(b);
Here, we used '0' because chars are actually represented by ASCII values. '0' is a char and represented by the value of 48.
We typed (a + '0') and in order to add these up, Java converted '0' to its ASCII value which is 48 and a is 1 so the sum is 49. Then what we did is:
(char)(49)
We casted int to char. ASCII equivalent of 49 is '1'. You can convert any digit to char this way and is smarter and better way than using .toString() method and then subtracting the digit by .charAt() method.
It seems like you are looking for the Character.forDigit method:
final int RADIX = 10;
int i = 4;
char ch = Character.forDigit(i, RADIX);
System.out.println(ch); // Prints '4'
There is also a method that can convert from a char back to an int:
int i2 = Character.digit(ch, RADIX);
System.out.println(i2); // Prints '4'
Note that by changing the RADIX you can also support hexadecimal (radix 16) and any radix up to 36 (or Character.MAX_RADIX as it is also known as).
int a = 1;
char b = (char) a;
System.out.println(b);
hola, well i went through the same problem but what i did was the following code.
int a = 1
char b = Integer.toString(a).charAt(0);
System.out.println(b);
With this you get the decimal value as a char type. I used charAt() with index 0 because the only value into that String is 'a' and as you know, the position of 'a' into that String start at 0.
Sorry if my english isn't well explained, hope it helps you.
you may want it to be printed as '1' or as 'a'.
In case you want '1' as input then :
int a = 1;
char b = (char)(a + '0');
System.out.println(b);
In case you want 'a' as input then :
int a = 1;
char b = (char)(a-1 + 'a');
System.out.println(b);
java turns the ascii value to char :)
int a = 1;
char b = (char) (a + 48);
In ASCII, every char have their own number. And char '0' is 48 for decimal, '1' is 49, and so on. So if
char b = '2';
int a = b = 50;
Nobody has answered the real "question" here: you ARE converting int to char correctly; in the ASCII table a decimal value of 01 is "start of heading", a non-printing character. Try looking up an ASCII table and converting an int value between 33 and 7E; that will give you characters to look at.
Whenever you type cast integer to char it will return the ascii value of that int (once go through the ascii table for better understanding)
int a=68;
char b=(char)a;
System.out.println(b);//it will return ascii value of 68
//output- D
If we are talking about class types - not primitives, the following trick has to be done:
Integer someInt;
Character someChar;
someChar = (char)Integer.parseInt(String.valueOf(someInt));
First, convert the int (or another type) to String,
int a = 1;
String value = String.valueOf(a);
Then, convert that String to char.
char newValue = value.charAt(0);
You can avoid empty output in this way...
System.out.println(newValue);
In java a char is an int. Your first snippet prints out the character corresponding to the value of 1 in the default character encoding scheme (which is probably Unicode). The Unicode character U+0001 is a non-printing character, which is why you don't see any output.
If you want to print out the character '1', you can look up the value of '1' in the encoding scheme you are using. In Unicode this is 49 (the same as ASCII). But this will only work for digits 0-9.
You might be better off using a String rather than a char, and using Java's built-in toString() method:
int a = 1;
String b = toString(a);
System.out.println(b);
This will work whatever your system encoding is, and will work for multi-digit numbers.
if you want to print ascii characters based on their ascii code and do not want to go beyond that (like unicode characters), you can define your variable as a byte, and then use the (char) convert. i.e.:
public static void main(String[] args) {
byte b = 65;
for (byte i=b; i<=b+25; i++) {
System.out.print((char)i + ", ");
}
BTW, the ascii code for the letter 'A' is 65
Make sure the integer value is ASCII value of an alphabet/character.
If not then make it.
for e.g. if int i=1
then add 64 to it so that it becomes 65 = ASCII value of 'A'
Then use
char x = (char)i;
print x
// 'A' will be printed
There is one method by which int can be converted to char and even without using ASCII values.
Example:
int i = 2;
char ch = Integer.toString(i).charAt(0);
System.out.println(ch);
Explanation :
First the integer is converted to string and then by using String function charAt(), character is extracted from the string. As the integer only has one single digit, the index 0 is given to charAt() function.
My solution is for converting lower case alphabets (a-z) to (0-25) and vice versa.
My answer is for a specific use-case it is not generic solution my solution will help you if you want to store the frequency of character into an integer array of size 26 instead of using Hashmap<Character,Integer>
----> for converting 0 to 25 into a-z
char ch=(char)(0+'a'); // output 'a' // input 0(as integer)
char ch=(char)(25+'a'); // output 'z' // input 25(as integer)
---->for converting a to z into 0-25
int freq='a'-'a' // output 0 // input 'a'
int freq='b'-'a' // output 1 // input 'b'
int freq='c'-'a' // output 2 // input 'c'
int freq='z'-'a' // output 25 // input 'z'
Again this approach will help you to get the frequency of characters as well as characters
public class Main
{
public static void main(String[] args) {
String s="rajatfddfdf";
int freq[]= new int[26];
for(int i=0;i<s.length();i++){
char characterAtIndex=s.charAt(i);
freq[characterAtIndex-'a']+=1;
}
for(int i=0;i<26;i++){
System.out.println((char)('a'+i)+" frequency="+freq[i]);
}
}
}
by using the above code we can get the frequency as well as character using integer array of size 26 . We can write if-else logic if you don't want to include the character with frequency 0.
look at the following program for complete conversion concept
class typetest{
public static void main(String args[]){
byte a=1,b=2;
char c=1,d='b';
short e=3,f=4;
int g=5,h=6;
float i;
double k=10.34,l=12.45;
System.out.println("value of char variable c="+c);
// if we assign an integer value in char cariable it's possible as above
// but it's not possible to assign int value from an int variable in char variable
// (d=g assignment gives error as incompatible type conversion)
g=b;
System.out.println("char to int conversion is possible");
k=g;
System.out.println("int to double conversion is possible");
i=h;
System.out.println("int to float is possible and value of i = "+i);
l=i;
System.out.println("float to double is possible");
}
}
hope ,it will help at least something
If you want to convert a character to its corresponding integer, you can do something like this:
int a = (int) 'a';
char b = (char) a;
System.out.println(b);
This happens because in ASCII there are some items that can not be printed normally.
For example, numbers 97 to 122 are integers corresponding to the lowercase letters a to z.
public class String_Store_In_Array
{
public static void main(String[] args)
{
System.out.println(" Q.37 Can you store string in array of integers. Try it.");
String str="I am Akash";
int arr[]=new int[str.length()];
char chArr[]=str.toCharArray();
char ch;
for(int i=0;i<str.length();i++)
{
arr[i]=chArr[i];
}
System.out.println("\nI have stored it in array by using ASCII value");
for(int i=0;i<arr.length;i++)
{
System.out.print(" "+arr[i]);
}
System.out.println("\nI have stored it in array by using ASCII value to original content");
for(int i=0;i<arr.length;i++)
{
ch=(char)arr[i];
System.out.print(" "+ch);
}
}
}

Extract the last 2 digits of an integer (Java)

I have some code that takes an integer and extracts the last 2 numbers and prints them. For example, if I input 10000001, 01 should be the printout/ output. The problem here is that for some reason the output of the program is 1. I am not sure why the output shows up as a single digit.
public class Main {
public static void main(String[] args) {
double num = 10000001;
double digit = num % 100;
System.out.println(digit);
}
}
Your problem is simple and has a simple answer.
In java int num = 1; is same as int num = 01; (both mean same 1 for the compiler) so when you are using % 100 with your number it returns 01 which is nothing but 1 for java as you are storing it to a double data type variable (actually when you use double in this line double digit = num % 100; it prints 1.0 , so you should use int here int digit = num % 100; to remove the decimal point).
So using int digit = num % 100; will work and give you desired results with all number except numbers having a 0 before a number.
To solve your problem completely you can use String class.
String str = num+""; //connverting int to string
String digit = str.substring(str.length()-2, str.length());
This is because your calculated value is "01". which while display java is considering it as "1". You can use padding but have to convert that integer to a string.
public static void main(String []args){
double num = 10000001;
int digit =(int) num%100;
String padding =String.format("%02d",digit);
System.out.println(padding);
}
Or If you are taking binary numbers better to use string and then displaying last two numbers using length() on string.
Another method you can try out is extracting the last two digits and printing as string.
int num = 10001;
int last_digit = num % 10; // extract the last digit
num /= 10; // modify the original number
int second_to_last_digit = num % 10; // extract second last digit
String str = String.valueOf(second_to_last_digit) + String.valueOf(last_digit); // convert and add strings
System.out.println(str); // print desired string

java converting char to int [duplicate]

Can someone please explain to me what is going on here:
char c = '+';
int i = (int)c;
System.out.println("i: " + i + " ch: " + Character.getNumericValue(c));
This prints i: 43 ch:-1. Does that mean I have to rely on primitive conversions to convert char to int? So how can I convert a Character to Integer?
Edit: Yes I know Character.getNumericValue returns -1 if it is not a numeric value and that makes sense to me. The question is: why does doing primitive conversions return 43?
Edit2: 43 is the ASCII for +, but I would expect the cast to not succeed just like getNumericValue did not succeed. Otherwise that means there are two semantic equivalent ways to perform the same operation but with different results?
Character.getNumericValue(c)
The java.lang.Character.getNumericValue(char ch) returns the int value that the specified Unicode character represents. For example, the character '\u216C' (the roman numeral fifty) will return an int with a value of 50.
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.
This method returns the numeric value of the character, as a
nonnegative int value;
-2 if the character has a numeric value that is not a nonnegative integer;
-1 if the character has no numeric value.
And here is the link.
As the documentation clearly states, Character.getNumericValue() returns the character's value as a digit.
It returns -1 if the character is not a digit.
If you want to get the numeric Unicode code point of a boxed Character object, you'll need to unbox it first:
int value = (int)c.charValue();
Try any one of the below. These should work:
int a = Character.getNumericValue('3');
int a = Integer.parseInt(String.valueOf('3');
From the Javadoc for Character#getNumericValue:
If the character does not have a numeric value, then -1 is returned.
If the character has a numeric value that cannot be represented as a
nonnegative integer (for example, a fractional value), then -2 is
returned.
The character + does not have a numeric value, so you're getting -1.
Update:
The reason that primitive conversion is giving you 43 is that the the character '+' is encoded as the integer 43.
43 is the dec ascii number for the "+" symbol. That explains why you get a 43 back.
http://en.wikipedia.org/wiki/ASCII
public class IntergerParser {
public static void main(String[] args){
String number = "+123123";
System.out.println(parseInt(number));
}
private static int parseInt(String number){
char[] numChar = number.toCharArray();
int intValue = 0;
int decimal = 1;
for(int index = numChar.length ; index > 0 ; index --){
if(index == 1 ){
if(numChar[index - 1] == '-'){
return intValue * -1;
} else if(numChar[index - 1] == '+'){
return intValue;
}
}
intValue = intValue + (((int)numChar[index-1] - 48) * (decimal));
System.out.println((int)numChar[index-1] - 48+ " " + (decimal));
decimal = decimal * 10;
}
return intValue;
}

Converting int to char in same integer format [duplicate]

Below is a code snippet,
int a = 1;
char b = (char) a;
System.out.println(b);
But what I get is empty output.
int a = '1';
char b = (char) a;
System.out.println(b);
I will get 1 as my output.
Can somebody explain this? And if I want to convert an int to a char as in the first snippet, what should I do?
int a = 1;
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 1 (start-of-heading char, which isn't printable; see this table: C0 Controls and Basic Latin, same as ASCII)
int a = '1';
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 49 (one corresponding to '1')
If you want to convert a digit (0-9), you can add 48 to it and cast, or something like Character.forDigit(a, 10);.
If you want to convert an int seen as a Unicode code point, you can use Character.toChars(48) for example.
My answer is similar to jh314's answer but I'll explain a little deeper.
What you should do in this case is:
int a = 1;
char b = (char)(a + '0');
System.out.println(b);
Here, we used '0' because chars are actually represented by ASCII values. '0' is a char and represented by the value of 48.
We typed (a + '0') and in order to add these up, Java converted '0' to its ASCII value which is 48 and a is 1 so the sum is 49. Then what we did is:
(char)(49)
We casted int to char. ASCII equivalent of 49 is '1'. You can convert any digit to char this way and is smarter and better way than using .toString() method and then subtracting the digit by .charAt() method.
It seems like you are looking for the Character.forDigit method:
final int RADIX = 10;
int i = 4;
char ch = Character.forDigit(i, RADIX);
System.out.println(ch); // Prints '4'
There is also a method that can convert from a char back to an int:
int i2 = Character.digit(ch, RADIX);
System.out.println(i2); // Prints '4'
Note that by changing the RADIX you can also support hexadecimal (radix 16) and any radix up to 36 (or Character.MAX_RADIX as it is also known as).
int a = 1;
char b = (char) a;
System.out.println(b);
hola, well i went through the same problem but what i did was the following code.
int a = 1
char b = Integer.toString(a).charAt(0);
System.out.println(b);
With this you get the decimal value as a char type. I used charAt() with index 0 because the only value into that String is 'a' and as you know, the position of 'a' into that String start at 0.
Sorry if my english isn't well explained, hope it helps you.
you may want it to be printed as '1' or as 'a'.
In case you want '1' as input then :
int a = 1;
char b = (char)(a + '0');
System.out.println(b);
In case you want 'a' as input then :
int a = 1;
char b = (char)(a-1 + 'a');
System.out.println(b);
java turns the ascii value to char :)
int a = 1;
char b = (char) (a + 48);
In ASCII, every char have their own number. And char '0' is 48 for decimal, '1' is 49, and so on. So if
char b = '2';
int a = b = 50;
Nobody has answered the real "question" here: you ARE converting int to char correctly; in the ASCII table a decimal value of 01 is "start of heading", a non-printing character. Try looking up an ASCII table and converting an int value between 33 and 7E; that will give you characters to look at.
Whenever you type cast integer to char it will return the ascii value of that int (once go through the ascii table for better understanding)
int a=68;
char b=(char)a;
System.out.println(b);//it will return ascii value of 68
//output- D
If we are talking about class types - not primitives, the following trick has to be done:
Integer someInt;
Character someChar;
someChar = (char)Integer.parseInt(String.valueOf(someInt));
First, convert the int (or another type) to String,
int a = 1;
String value = String.valueOf(a);
Then, convert that String to char.
char newValue = value.charAt(0);
You can avoid empty output in this way...
System.out.println(newValue);
In java a char is an int. Your first snippet prints out the character corresponding to the value of 1 in the default character encoding scheme (which is probably Unicode). The Unicode character U+0001 is a non-printing character, which is why you don't see any output.
If you want to print out the character '1', you can look up the value of '1' in the encoding scheme you are using. In Unicode this is 49 (the same as ASCII). But this will only work for digits 0-9.
You might be better off using a String rather than a char, and using Java's built-in toString() method:
int a = 1;
String b = toString(a);
System.out.println(b);
This will work whatever your system encoding is, and will work for multi-digit numbers.
if you want to print ascii characters based on their ascii code and do not want to go beyond that (like unicode characters), you can define your variable as a byte, and then use the (char) convert. i.e.:
public static void main(String[] args) {
byte b = 65;
for (byte i=b; i<=b+25; i++) {
System.out.print((char)i + ", ");
}
BTW, the ascii code for the letter 'A' is 65
Make sure the integer value is ASCII value of an alphabet/character.
If not then make it.
for e.g. if int i=1
then add 64 to it so that it becomes 65 = ASCII value of 'A'
Then use
char x = (char)i;
print x
// 'A' will be printed
There is one method by which int can be converted to char and even without using ASCII values.
Example:
int i = 2;
char ch = Integer.toString(i).charAt(0);
System.out.println(ch);
Explanation :
First the integer is converted to string and then by using String function charAt(), character is extracted from the string. As the integer only has one single digit, the index 0 is given to charAt() function.
My solution is for converting lower case alphabets (a-z) to (0-25) and vice versa.
My answer is for a specific use-case it is not generic solution my solution will help you if you want to store the frequency of character into an integer array of size 26 instead of using Hashmap<Character,Integer>
----> for converting 0 to 25 into a-z
char ch=(char)(0+'a'); // output 'a' // input 0(as integer)
char ch=(char)(25+'a'); // output 'z' // input 25(as integer)
---->for converting a to z into 0-25
int freq='a'-'a' // output 0 // input 'a'
int freq='b'-'a' // output 1 // input 'b'
int freq='c'-'a' // output 2 // input 'c'
int freq='z'-'a' // output 25 // input 'z'
Again this approach will help you to get the frequency of characters as well as characters
public class Main
{
public static void main(String[] args) {
String s="rajatfddfdf";
int freq[]= new int[26];
for(int i=0;i<s.length();i++){
char characterAtIndex=s.charAt(i);
freq[characterAtIndex-'a']+=1;
}
for(int i=0;i<26;i++){
System.out.println((char)('a'+i)+" frequency="+freq[i]);
}
}
}
by using the above code we can get the frequency as well as character using integer array of size 26 . We can write if-else logic if you don't want to include the character with frequency 0.
look at the following program for complete conversion concept
class typetest{
public static void main(String args[]){
byte a=1,b=2;
char c=1,d='b';
short e=3,f=4;
int g=5,h=6;
float i;
double k=10.34,l=12.45;
System.out.println("value of char variable c="+c);
// if we assign an integer value in char cariable it's possible as above
// but it's not possible to assign int value from an int variable in char variable
// (d=g assignment gives error as incompatible type conversion)
g=b;
System.out.println("char to int conversion is possible");
k=g;
System.out.println("int to double conversion is possible");
i=h;
System.out.println("int to float is possible and value of i = "+i);
l=i;
System.out.println("float to double is possible");
}
}
hope ,it will help at least something
If you want to convert a character to its corresponding integer, you can do something like this:
int a = (int) 'a';
char b = (char) a;
System.out.println(b);
This happens because in ASCII there are some items that can not be printed normally.
For example, numbers 97 to 122 are integers corresponding to the lowercase letters a to z.
public class String_Store_In_Array
{
public static void main(String[] args)
{
System.out.println(" Q.37 Can you store string in array of integers. Try it.");
String str="I am Akash";
int arr[]=new int[str.length()];
char chArr[]=str.toCharArray();
char ch;
for(int i=0;i<str.length();i++)
{
arr[i]=chArr[i];
}
System.out.println("\nI have stored it in array by using ASCII value");
for(int i=0;i<arr.length;i++)
{
System.out.print(" "+arr[i]);
}
System.out.println("\nI have stored it in array by using ASCII value to original content");
for(int i=0;i<arr.length;i++)
{
ch=(char)arr[i];
System.out.print(" "+ch);
}
}
}

Convert int to char in java

Below is a code snippet,
int a = 1;
char b = (char) a;
System.out.println(b);
But what I get is empty output.
int a = '1';
char b = (char) a;
System.out.println(b);
I will get 1 as my output.
Can somebody explain this? And if I want to convert an int to a char as in the first snippet, what should I do?
int a = 1;
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 1 (start-of-heading char, which isn't printable; see this table: C0 Controls and Basic Latin, same as ASCII)
int a = '1';
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 49 (one corresponding to '1')
If you want to convert a digit (0-9), you can add 48 to it and cast, or something like Character.forDigit(a, 10);.
If you want to convert an int seen as a Unicode code point, you can use Character.toChars(48) for example.
My answer is similar to jh314's answer but I'll explain a little deeper.
What you should do in this case is:
int a = 1;
char b = (char)(a + '0');
System.out.println(b);
Here, we used '0' because chars are actually represented by ASCII values. '0' is a char and represented by the value of 48.
We typed (a + '0') and in order to add these up, Java converted '0' to its ASCII value which is 48 and a is 1 so the sum is 49. Then what we did is:
(char)(49)
We casted int to char. ASCII equivalent of 49 is '1'. You can convert any digit to char this way and is smarter and better way than using .toString() method and then subtracting the digit by .charAt() method.
It seems like you are looking for the Character.forDigit method:
final int RADIX = 10;
int i = 4;
char ch = Character.forDigit(i, RADIX);
System.out.println(ch); // Prints '4'
There is also a method that can convert from a char back to an int:
int i2 = Character.digit(ch, RADIX);
System.out.println(i2); // Prints '4'
Note that by changing the RADIX you can also support hexadecimal (radix 16) and any radix up to 36 (or Character.MAX_RADIX as it is also known as).
int a = 1;
char b = (char) a;
System.out.println(b);
hola, well i went through the same problem but what i did was the following code.
int a = 1
char b = Integer.toString(a).charAt(0);
System.out.println(b);
With this you get the decimal value as a char type. I used charAt() with index 0 because the only value into that String is 'a' and as you know, the position of 'a' into that String start at 0.
Sorry if my english isn't well explained, hope it helps you.
you may want it to be printed as '1' or as 'a'.
In case you want '1' as input then :
int a = 1;
char b = (char)(a + '0');
System.out.println(b);
In case you want 'a' as input then :
int a = 1;
char b = (char)(a-1 + 'a');
System.out.println(b);
java turns the ascii value to char :)
int a = 1;
char b = (char) (a + 48);
In ASCII, every char have their own number. And char '0' is 48 for decimal, '1' is 49, and so on. So if
char b = '2';
int a = b = 50;
Nobody has answered the real "question" here: you ARE converting int to char correctly; in the ASCII table a decimal value of 01 is "start of heading", a non-printing character. Try looking up an ASCII table and converting an int value between 33 and 7E; that will give you characters to look at.
Whenever you type cast integer to char it will return the ascii value of that int (once go through the ascii table for better understanding)
int a=68;
char b=(char)a;
System.out.println(b);//it will return ascii value of 68
//output- D
If we are talking about class types - not primitives, the following trick has to be done:
Integer someInt;
Character someChar;
someChar = (char)Integer.parseInt(String.valueOf(someInt));
First, convert the int (or another type) to String,
int a = 1;
String value = String.valueOf(a);
Then, convert that String to char.
char newValue = value.charAt(0);
You can avoid empty output in this way...
System.out.println(newValue);
In java a char is an int. Your first snippet prints out the character corresponding to the value of 1 in the default character encoding scheme (which is probably Unicode). The Unicode character U+0001 is a non-printing character, which is why you don't see any output.
If you want to print out the character '1', you can look up the value of '1' in the encoding scheme you are using. In Unicode this is 49 (the same as ASCII). But this will only work for digits 0-9.
You might be better off using a String rather than a char, and using Java's built-in toString() method:
int a = 1;
String b = toString(a);
System.out.println(b);
This will work whatever your system encoding is, and will work for multi-digit numbers.
if you want to print ascii characters based on their ascii code and do not want to go beyond that (like unicode characters), you can define your variable as a byte, and then use the (char) convert. i.e.:
public static void main(String[] args) {
byte b = 65;
for (byte i=b; i<=b+25; i++) {
System.out.print((char)i + ", ");
}
BTW, the ascii code for the letter 'A' is 65
Make sure the integer value is ASCII value of an alphabet/character.
If not then make it.
for e.g. if int i=1
then add 64 to it so that it becomes 65 = ASCII value of 'A'
Then use
char x = (char)i;
print x
// 'A' will be printed
There is one method by which int can be converted to char and even without using ASCII values.
Example:
int i = 2;
char ch = Integer.toString(i).charAt(0);
System.out.println(ch);
Explanation :
First the integer is converted to string and then by using String function charAt(), character is extracted from the string. As the integer only has one single digit, the index 0 is given to charAt() function.
My solution is for converting lower case alphabets (a-z) to (0-25) and vice versa.
My answer is for a specific use-case it is not generic solution my solution will help you if you want to store the frequency of character into an integer array of size 26 instead of using Hashmap<Character,Integer>
----> for converting 0 to 25 into a-z
char ch=(char)(0+'a'); // output 'a' // input 0(as integer)
char ch=(char)(25+'a'); // output 'z' // input 25(as integer)
---->for converting a to z into 0-25
int freq='a'-'a' // output 0 // input 'a'
int freq='b'-'a' // output 1 // input 'b'
int freq='c'-'a' // output 2 // input 'c'
int freq='z'-'a' // output 25 // input 'z'
Again this approach will help you to get the frequency of characters as well as characters
public class Main
{
public static void main(String[] args) {
String s="rajatfddfdf";
int freq[]= new int[26];
for(int i=0;i<s.length();i++){
char characterAtIndex=s.charAt(i);
freq[characterAtIndex-'a']+=1;
}
for(int i=0;i<26;i++){
System.out.println((char)('a'+i)+" frequency="+freq[i]);
}
}
}
by using the above code we can get the frequency as well as character using integer array of size 26 . We can write if-else logic if you don't want to include the character with frequency 0.
look at the following program for complete conversion concept
class typetest{
public static void main(String args[]){
byte a=1,b=2;
char c=1,d='b';
short e=3,f=4;
int g=5,h=6;
float i;
double k=10.34,l=12.45;
System.out.println("value of char variable c="+c);
// if we assign an integer value in char cariable it's possible as above
// but it's not possible to assign int value from an int variable in char variable
// (d=g assignment gives error as incompatible type conversion)
g=b;
System.out.println("char to int conversion is possible");
k=g;
System.out.println("int to double conversion is possible");
i=h;
System.out.println("int to float is possible and value of i = "+i);
l=i;
System.out.println("float to double is possible");
}
}
hope ,it will help at least something
If you want to convert a character to its corresponding integer, you can do something like this:
int a = (int) 'a';
char b = (char) a;
System.out.println(b);
This happens because in ASCII there are some items that can not be printed normally.
For example, numbers 97 to 122 are integers corresponding to the lowercase letters a to z.
public class String_Store_In_Array
{
public static void main(String[] args)
{
System.out.println(" Q.37 Can you store string in array of integers. Try it.");
String str="I am Akash";
int arr[]=new int[str.length()];
char chArr[]=str.toCharArray();
char ch;
for(int i=0;i<str.length();i++)
{
arr[i]=chArr[i];
}
System.out.println("\nI have stored it in array by using ASCII value");
for(int i=0;i<arr.length;i++)
{
System.out.print(" "+arr[i]);
}
System.out.println("\nI have stored it in array by using ASCII value to original content");
for(int i=0;i<arr.length;i++)
{
ch=(char)arr[i];
System.out.print(" "+ch);
}
}
}

Categories