• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Int = Char

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not positive I follow you exactly but I think you want something like:

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can check if the number as a character is actually a number"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic