wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes java.lang.NumberFormatException: For input string: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.NumberFormatException: For input string: "139.56"" Watch "java.lang.NumberFormatException: For input string: "139.56"" New topic
Author

java.lang.NumberFormatException: For input string: "139.56"

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Hi,
I am trying to convert a String Object to Byte Object.
But, I am getting error as:


My code goes here:


I don't know why I am getting this error. I want to convert this String value to Byet Object.
Can anyone please help me ?

With Regards,
Anu
Randall Fairman
Greenhorn

Joined: Apr 18, 2011
Posts: 29

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().
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
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)

How to do this? Please help!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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.

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
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?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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?
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Anu satya wrote:
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?



Look at http://download.oracle.com/javase/6/docs/api/java/lang/Double.html#byteValue%28%29

First convert from String to Double using the .parseDouble(String s) method. Then take the Double and call .byteValue().


But then again... you will only get the Byte part of the value. Not a byterepresentation of the Double value.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

A byte in Java is an 8-bit signed integer. It can only contain whole numbers between -128 and 127.

Before we can answer your question, you'll need to answer this: Suppose the string contains "139.56", as in your example. What value would you expect the byte to have after converting it?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Is this a serialization problem? Do you want to send a Double value as a byte array?? Because Byte and Byte array is not really the same thing...
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Jesper de Jong wrote:What value would you expect the byte to have after converting it?


I would expect respective byte value of 139.56.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

If you have a jar that can only hold 2 liters, how would you put 3 liters into it?

You can't.

it's the same thing. A Byte cannot store any value other than an integer between -128 and +127. So trying to put a value of 139 won't work. Putting a value of 8.7 can't be done.

"a byte value of 139.56" simply does not make sense. Byte values are integers. You can store the value of 1, 2, 3, 4...up to 127. but it is impossible to store 1.2 in a byte.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4372
    
    8

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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.
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
HI,
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.



please let me know how to do it.



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Anu satya wrote:I need to convert that into Byte value


That's the same question again. Stop asking it and go back to wherever you got that requirement from. It's clear that you have misunderstood what you were told to do.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

WHY are you insisting on using a Byte? Why can't you use Float and its valueof() method?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Anu satya wrote:
Jesper de Jong wrote:What value would you expect the byte to have after converting it?

I would expect respective byte value of 139.56.

Did you understand what I and others wrote? Why do you still not realize that a value like 139.56 is impossible to fit into a byte?

I find it hard to believe that your AmountDTO stores the amount as a byte, as you are suggesting:

Are you sure that is correct? What does your AmountDTO class look like, especially its setAmount method? Does it really take a byte as an argument?

What is the exact type of the column in the database that the amount needs to be stored in?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4372
    
    8

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".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: java.lang.NumberFormatException: For input string: "139.56"