How to determine whether char or int

G

Guest

Hi,
Given a string, how do you determine whether its of primitive type int or
primitive type char?

Thanks
 
W

Wiseguy

Hi,
Given a string, how do you determine whether its of primitive type int or
primitive type char?

Thanks

It's niether...it is a string until you do some tests on it to determine
whether is can be represented in some other format.
 
E

Edwin Martin

Given a string, how do you determine whether its of primitive type int or
primitive type char?

Java is not C!

In OOP you don't have to know the inside of an object. Maybe the String
is stored as gzipped EBCDIC. You don't care. As long as the API is
correctly implemented. The fact that Java supports Unicode gives a clue,
though.

Edwin Martin
 
T

Tom Dyess

Hi,
Given a string, how do you determine whether its of primitive type int or
primitive type char?

Thanks

I prefer this method. If it doesn't cast properly using the Integer object,
it will throw an exception.

public static boolean isNumeric(String s) {
try {
Integer i = new Integer(s);
return true;
} catch (Exception e) {
return false;
}
}
 
R

R.F. Pels

Given a string, how do you determine whether its of primitive type int or
primitive type char?

Can you explain a bit further, for example with a code snippet?
 
L

Lee Fesperman

Tom said:
I prefer this method. If it doesn't cast properly using the Integer object,
it will throw an exception.

public static boolean isNumeric(String s) {
try {
Integer i = new Integer(s);
return true;
} catch (Exception e) {
return false;
}
}

Why not just use Integer.parseInt(s)? No need to create a throwaway Integer object. In
either case, don't bother assigning it to anything.

Lastly, use the specific exception, NumberFormatException. It's clearer.

public static boolean isNumeric(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}

.... much nicer ;^)
 
T

Tom Dyess

Lee Fesperman said:
Why not just use Integer.parseInt(s)? No need to create a throwaway
Integer object. In
either case, don't bother assigning it to anything.

Lastly, use the specific exception, NumberFormatException. It's clearer.

public static boolean isNumeric(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}

... much nicer ;^)

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)

Kewl, didn't even know that existed.
 
L

Lee Fesperman

Tom said:
Kewl, didn't even know that existed.

It's perfect when you need the value but only as a primitive.

BTW, using java.lang.Exception instead of NumberFormatException would be proper if you
also wanted to cover a null reference.
 
R

Ryan Stewart

[...]
BTW, using java.lang.Exception instead of NumberFormatException would be
proper if you
also wanted to cover a null reference.
Not really. Then you'd add a catch block for a NullPointerException or mention
in the documentation that if you pass a null reference, the method will throw a
NullPointerException, which should pretty well be common sense anyhow.
 
L

Lee Fesperman

Ryan said:
[...]
BTW, using java.lang.Exception instead of NumberFormatException would be
proper if you
also wanted to cover a null reference.
Not really. Then you'd add a catch block for a NullPointerException or mention
in the documentation that if you pass a null reference, the method will throw a
NullPointerException, which should pretty well be common sense anyhow.

Yep, I'll go along with that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top