This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The error occurs in the line Byte x = Byte.valueOf(f);. The String f is not a valid byte value; it's a float or double. I'm not sure what your intent is. Maybe you want to call Double.doubleToLongBits() after parsing f with Double.valueOf().
If you read the API, it says "The argument is interpreted as representing a signed decimal byte, exactly as if the argument were given to the parseByte(java.lang.String) method." If you then follow the link to that method, you'll see this:
"The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value."
The fact that you have a decimal point in your string causes the exception.
A byte data type is an integer type. Trying to force a floating-point number into it is going to cause problems.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Mar 17, 2005
Thanks for your reply.
My interest is to convert a String value to Byte Object. String values might have values like "100" , "19.9" etc... (NEver negative)
The API clearly states that you CAN'T put a number like "19.9" into a byte. the fact that there is a decimal point makes it invalid. A Byte can only hold integer values, and you are trying to stick in a number that is not an integer.
You have a couple of options...
1) Catch the exception and print and error message of some kind
2) Parse the String first to make sure you don't have a decimal point
3) Don't use a Byte, but some other wrapper that WILL accept a fractional number.
Joined: Mar 17, 2005
Campbell Ritchie wrote:You can't convert a String to a Byte like that. There are ways of converting a String to a byte, but that is something different.
Any other work around for this problem? how about converting String to Double and then to Byte? is it possible?
you will again have the same problem. you CANNOT store the fractional part of a number in a Byte. the Byte class is not designed to do that.
You are effectively saying "how can I drive my car to the moon?" We are telling you that a car is not designed to travel to the moon, and you are saying "Well, what if I seal the windows so air doesn't leak out - then can I drive it to the moon?" The answer is still "no".
WHY are you insisting on putting these values into a Byte? If you know you can get fractional values, and you want to store them, why don't you just use a Float or a Double?
or why don't you tell us what you are really trying to do, and not ask how to force an inappropriate solution onto a problem?
Anu satya wrote:I would expect respective byte value of 139.56.
Do you know what a byte is? It's an integer type. As such, it can only contain integers. 139 is an integer, so is 140. But 139.56 isn't. So if you want that converting into a byte you need to define exactly how you expect it to work, and the Byte class can't guess that for you.
I suspect, though, that this is just step towards what you are actually wanting to achieve, and what you ought to be doing is something completely different. So as Fred said earlier, if you tell us what the real requirement is we might be able to help you with that.
Joined: Oct 13, 2005
Anu satya wrote: . . . Any other work around for this problem? . . .
Why don't you believe us all when we tell you it can't be done? It's almost as if you thought we were hiding something, but we aren't.
Joined: Mar 17, 2005
Sorry, I was asking same question again and again.
As you said, it is just a step towards achieving my goal. In my project, transaction amount is coming in xml file. I am reading that transaction amount and putting that value to DB. so, amount is in string format. I need to convert that into Byte value and instantiate Data Transfer Object.
It seems to me there are two possibilities here as to what's going on.
1. The database isn't actually using a byte for a column type, but is actually using a byte array, or a floating-point type. If this is true, then the DTO should also be using a byte array or a floating-point type, and you've misunderstood what's required. If it's a floating-point type (like double), then there's no problem - Double.parseDouble() will do what you need. If it's a byte array, then you need some additional information about how it should be converted to a byte array, as there are a few different ways of doing it.
2. Or, it really is using a byte. In this case, as we've been saying, it's simply not possible to store a fractional number in it stored exactly. In this case, you need to know more about the requirements. Either it's invalid input, and an error should be thrown, or some business rule is needed to define what should be done (e.g. "round it to the nearest integer").
So you need to understand that this is not a Java problem. If by magic you managed to fit it into a "Java byte", you'd hit the same problem when you inserted it into the database. The problem is the definition of "byte" - and you can't change that. If you're looking for a workaround, the workaround is "you need to do something else".
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: java.lang.NumberFormatException: For input string: "139.56"