Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Int = Char  RSS feed

 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way in wich you can do.

if and int value = a char value then you can output an error message.
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not positive I follow you exactly but I think you want something like:

 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am sure too...but lets see...i tried the following:

public class CharAndInt {
public static void main(String args[]) {
int i = 67;
try {
char s = (char)i;
System.out.println("the char is: " + s);
} catch (Exception e) {
System.out.println("int out of range for char!!!");
}
}
}

even if I replace i with the max value of an int....it still gives a "?" as the result...dunno why...am I doing something wrong?
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Preetham Chandrasekhar:
even if I replace i with the max value of an int....it still gives a "?" as the result...dunno why...am I doing something wrong?


The code you have will never throw an Exception. Casting in the way you are doing it is truncation and is unchecked. If the integer is too large for the char then the high order bits are truncated.

If you would like to verify first that the integer is in range you would need to compare it against Character.MAX_VALUE - 32767.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What are you trying to do? See if an int value is in the possible range of
values of a char? Recall that casting may truncate, but it will never throw
an exception. Type char is essentially a 16-bit unsigned integer type, so
if you want to see if an int value is in range, test to see if it is in [0, 0xffff]
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it!...this was a gud one....got a chance to get part of my int-char concepts cleared!

public class CharAndInt {
public static void main(String args[]) {
int i = 123;
if((i > Character.MAX_VALUE) || (i < Character.MIN_VALUE)) {
System.out.println("the int has no char match!!");
} else {
System.out.println("The matching char value is: " + (char)i);
}
}
}

so this kinda works better i guess
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is i got a program where you input a number etc, but when i input a character value i want the program to throw an error message.
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cant you use Integer.parseInt and check if its an int...and if not...it would automatically throw a NumberFormatException...
 
Ray Horn
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have the whole string then use the wrapper class to convert the string to the primitive and it will check that the format is correct (throws NumberFormatException if bad char in string).

ex:
try{
String intString = "134";
int num = Integer.parseInt(intString);
} catch (NumberFormatException ex) {
//do something on format error
}

But if your checking one char at a time then the Character.isDigit() will let you know if you have a digit or something else.
[ November 01, 2005: Message edited by: Ray Horn ]
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can check if the number as a character is actually a number"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!