exception handling problem

Z

zcraven

public void setPlayerHeight(int height_in_cms) throws
IllegalArgumentException
{
Integer dwarf = new Integer(70);
Integer giant = new Integer(270);
Integer zero = new Integer(0);
/* if ((Integer (height_in_cms).intValue)=0){
throw new IllegalStateException("height cannot be blank.");
} */

if (height_in_cms == zero.intValue()){
throw new IllegalStateException("height cannot be a negative
number.");
}
if (height_in_cms <= dwarf.intValue()){
throw new IllegalArgumentException("height cannot be less than
70cms");
}
if (height_in_cms >= giant.intValue()){
throw new IllegalArgumentException("height cannot be above 270cms");
}
playerHeight = height_in_cms;
}


Why doesn't this throw an error when I enter the height as too tall/short
etc? Weird...
 
C

Carl Howells

zcraven said:
public void setPlayerHeight(int height_in_cms) throws
IllegalArgumentException
{
Integer dwarf = new Integer(70);
Integer giant = new Integer(270);
Integer zero = new Integer(0);
/* if ((Integer (height_in_cms).intValue)=0){
throw new IllegalStateException("height cannot be blank.");
} */

if (height_in_cms == zero.intValue()){
throw new IllegalStateException("height cannot be a negative
number.");
}
if (height_in_cms <= dwarf.intValue()){
throw new IllegalArgumentException("height cannot be less than
70cms");
}
if (height_in_cms >= giant.intValue()){
throw new IllegalArgumentException("height cannot be above 270cms");
}
playerHeight = height_in_cms;
}


Why doesn't this throw an error when I enter the height as too tall/short
etc? Weird...

That code doesn't even come close to compiling. It has multiple errors,
both typographical and type-based, as well as being incomplete. If you
want to ask a question about the behavior of a particular piece of code,
actually give us the piece of code you're using. That's very clearly
not it.
 
C

Chris Smith

(e-mail address removed) says...
That code doesn't even come close to compiling. It has multiple errors,
both typographical and type-based, as well as being incomplete. If you
want to ask a question about the behavior of a particular piece of code,
actually give us the piece of code you're using. That's very clearly
not it.

Admittedly the use of Integer is a bit odd, but the code does compile
(modulo the wrapping from the OP's news reader). What do you see wrong
with it?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Carl Howells

Chris said:
(e-mail address removed) says...



Admittedly the use of Integer is a bit odd, but the code does compile
(modulo the wrapping from the OP's news reader). What do you see wrong
with it?

Hmm. Oops, I should have looked more carefully. The line I was looking
at, with MANY errors in it, is commented out. I suppose that's why it's
commented out...
 
Z

zcraven

Carl Howells said:
Hmm. Oops, I should have looked more carefully. The line I was looking
at, with MANY errors in it, is commented out. I suppose that's why it's
commented out...

I got it to work ok (the problem was in the calling program), but the
problem I have now is comparing an INT to a null value. how do you do that?

if int x == null {throw errormsg}

it says i cant compare an int to a null.

zac
 
Y

Yogo

zcraven said:
I got it to work ok (the problem was in the calling program), but the
problem I have now is comparing an INT to a null value. how do you do
that?

if int x == null {throw errormsg}

it says i cant compare an int to a null.

No need to do the comparison. An int (like all the other primitives: byte,
short, long...) is never null. It always has a value. Only references can be
null.


Yogo
 
C

Chris Smith

(e-mail address removed) says...
I got it to work ok (the problem was in the calling program), but the
problem I have now is comparing an INT to a null value. how do you do that?

if int x == null {throw errormsg}

it says i cant compare an int to a null.

You can't compare an int to null. null is not a possible value for a
variable of type int, so such a comparison is pointless. The question
is why you think you need to do this comparison.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Z

zcraven

Chris Smith said:
(e-mail address removed) says...

You can't compare an int to null. null is not a possible value for a
variable of type int, so such a comparison is pointless. The question
is why you think you need to do this comparison.

well why do you have to do it with a string?

Just like I want to check that someone entered a value (string) for
'playername', I want to check they entered a value (int) for 'playerheight'.
 
G

Gurunath M.

Hi,
well why do you have to do it with a string?

There are two categories of Datatypes in Java. primitive
(consisting of int, float, double, char, boolean etc) and reference
(consisting of all the classes that you can see in the Java API and
more).
A string is a reference datatype.
A primitive datatype can never be null, while reference datatype,
which being a reference to an object, can be null.
Hence u have to Check for null value of String (or any reference )
but not for any primitive data type.
Try to go throught this link.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

Regards,
Gurunath M.
 
C

Chris Smith

(e-mail address removed) says...
well why do you have to do it with a string?

Just like I want to check that someone entered a value (string) for
'playername', I want to check they entered a value (int) for 'playerheight'.

The check you need should come before you convert the user input. You
will generally end up retrieving input as text (e.g., a String), and
then converting it to an integer later. Check to be sure that some data
was entered before you attempt that type conversion. Once you've got an
'int' typed value, there is no longer a possibility that it is null (but
you may not get that far; the conversion may throw NullPointerException,
NumberFormatException, or ParseException, depending on how you do it).

Depending on where you get the data from, there may be other answers as
well. For example, there's a quite specific method available to check
for SQL NULL values in data retrieved from a relational database.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top