• Post Reply Bookmark Topic Watch Topic
  • New Topic

casting from a JTextField to a float?  RSS feed

 
Jerry L Kreps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to do the following:

but I get an error msg saying "Error(1310,14): cannot cast java.lang.String to float"
So, how do I take decimal values in textboxes and add them up, then assign them to a variable?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try:

All the wrapper classes (Integer, Float, Double, etc.) have methods to parse a string and return a primitive value (or a new wrapper instance, if that is your desire...) Just be sure to handle NumberFormatExceptions!
 
Jerry L Kreps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel McNary:
try:

All the wrapper classes (Integer, Float, Double, etc.) have methods to parse a string and return a primitive value (or a new wrapper instance, if that is your desire...) Just be sure to handle NumberFormatExceptions!

Thanks, Joel, that did the trick!
I also discovered that the Java API doesn't appear to have a built in rounding function so I borrowed one off the web

I appreciate your reply!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!