Home arrow static arrow Java Programming [Archive] - convert empty value("") to Integer object
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - convert empty value("") to Integer object
This topic has 11 replies on 1 page.

Posts:30
Registered: 7/1/04
convert empty value("") to Integer object  
Aug 2, 2004 6:11 AM



 
Hi,
Is it possible to convert empty value to Integer object?Actually i had an xml element which is empty specified like <day\> and I want to store the value extracted from this as Integer object.I tried with Integer.valueOf(e.getchildText("day")) But is is giving NumberFormatException saying that "" is not valid.
Anybody help me how to solve this.

Thanks
 

Posts:5,965
Registered: 5/17/03
Re: convert empty value("") to Integer object  
Aug 2, 2004 6:14 AM (reply 1 of 11)



 
I guess you read the field as a String. Can't you just check first if the String is "" before you try to convert it to an Integer? If it is "" the you create the Integer of your choise.
 

Posts:319
Registered: 11/8/00
Re: convert empty value("") to Integer object  
Aug 2, 2004 6:15 AM (reply 2 of 11)



 
you could check to see if the string was equal to an empty string and if it was set the value to 0, else just do the code you already have.

String value = "";int intValue;if (value.equals("")){    intValue = 0;}else{    intValue = Integer.ParseInt(value);}
 

Posts:30
Registered: 7/1/04
Re: convert empty value("") to Integer object  
Aug 2, 2004 6:31 AM (reply 3 of 11)



 
Many thanks to both of you.
 

Posts:1,872
Registered: 2/12/03
Re: convert empty value("") to Integer object  
Aug 2, 2004 6:41 AM (reply 4 of 11)



 
you should use try and catch block
if the String n = "12d3" then the answer above would result in a NumberFormattedException

String n = "";int number = 0; try{    number = Integer.parseInt(n);}catch (NumberFormattedException ne){    // you can set the value of the number here, if "n" is a bad string (not an integer string)}

 

Posts:1,888
Registered: 22/08/01
Re: convert empty value("") to Integer object  
Aug 2, 2004 6:59 AM (reply 5 of 11)



 
if you know that you will be parsing empty string literals then you should test on them and not rely on try catch, Exceptions are for some exceptional, not for something you designed.
 

Posts:441
Registered: 2/25/04
Re: convert empty value("") to Integer object  
Aug 2, 2004 7:00 AM (reply 6 of 11)



 
Is it possible to convert empty value to Integer object?
No.

Actually i had an xml element which is empty specified like <day\> and I want to store the value extracted from this as Integer object.

Is empty valid according to your XML schema? If so, are the semantics recorded there?

What, according to your business logic, is the significant of day (are 0-6 sunday-saturday, or what?)?

Why do you want an Integer object anyway? If the absence of a value is a valid input, then maybe you're better off leaving it as a string, or possibly returning null.

Pete

 

Posts:1,872
Registered: 2/12/03
Re: convert empty value("") to Integer object  
Aug 3, 2004 7:24 AM (reply 7 of 11)



 
if you know that you will be parsing empty string literals then you should test on them and not rely on try
catch, Exceptions are for some exceptional, not for something you designed.

yes, but are you sure that only empty string and "integer string" are inputs??
rather be safe to catch all possible senerio than ddeploying the project to find out that it crashes on bad input
 

Posts:683
Registered: 12/5/97
Re: convert empty value("") to Integer object  
Aug 3, 2004 7:39 AM (reply 8 of 11)



 
Double and Float classes both have isNaN methods that indicate that the class does not actually represent a number. Had I my way, all numerical wrapper classes would have such a method for testing String values and there would be no exceptional situation in a parse:
if (Integer.isNaN(stringval)){   //something} else {   val = Integer.parseInt(stringval);}
For whatever reason (me thinks it was overlooked) there is no such functionality, so catching the thrown NumberFormatException is currently the best way to go.
 

Posts:6,750
Registered: 1/25/04
Re: convert empty value("") to Integer object  
Aug 3, 2004 8:08 AM (reply 9 of 11)



 
The problem is by just catching the exception you cannot tell the difference between "" and "Cookie Monster." The former should be 0 or whatever, the latter should be a problem.
 

Posts:441
Registered: 2/25/04
Re: convert empty value("") to Integer object  
Aug 3, 2004 9:35 AM (reply 10 of 11)



 
Double and Float classes both have isNaN methods that indicate that the class does not actually represent a number.

These methods indicate that the floating point value is NaN, the result you get when dividing zero by zero.

Had I my way, all numerical wrapper classes would have such a method for testing String values and there would be no exceptional situation in a parse

NaN is not an invalid floating point number, it represents results that do not correspond to intervals on the real number line.

if (Integer.isNaN(stringval)){ //something } else { val = Integer.parseInt(stringval);}

There is no equivalent to NaN in fixed precision integer representations, nor does NaN signify an invalid input to Double.parseDouble()*. Every int bit pattern corresponds exactly to a signed integer in the range -2147483648 to 2147483647.

Pete

* I seem to recall the input "NaN" not being valid at some point, at least to Double.valueOf in the 1.1 days, but can't find when it changed.

 

Posts:683
Registered: 12/5/97
Re: convert empty value("") to Integer object  
Aug 4, 2004 8:39 AM (reply 11 of 11)



 
Yes, NaN represents zero divided by zero, non-zero non-infinity divided by zero, infinity divided by infinity, any multiplication or division expression where either operand is NaN, etc., all as outlined in JLS section 15.17.

I'm sorry if I expressed myself in such a way as to leave my meaning vague, so I'll say it again without referring to JLS constructs whose current definitions don't align with my stated intent.

I'd like numerical wrapper classes to have methods that test Strings for numerical content without throwing exceptions.

Simple enough?
 
This topic has 11 replies on 1 page.