• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

number format exception

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, so ive just recently started learning java and programming in general and i've started my first ever project, however i've encountered an error and i just cant get my head around it due to lack of knowledge in the area. I'm currently making a currency converter and i need to make an option where the user can enter a British pound amount and click convert for it to convert, then have the option to enter their own exchange rate into the calculation and when convert is clicked then it will use the users inputted exchange rate instead of the one that i have set. Here is the code i have so far to convert GBP to USD, the program works when i input an amount and also input an exchange rate myself, however i dont know how to write the program to tell it to use the GBPtoUSD double variable whenever the ExchangeRateFrom input is empty. Can anyone please help me:



I get this error exception when converting without inputting an exchange rate (should be using the GBPtoUSD double when nothing is inputted to this box):






Other information that may help you understand the program better:

Heres a GIF of the error in action: https://gyazo.com/af8d681e8f5cf190b2ed2c61b53efa8e

Here is a different version of this code that i tried however i still get the same error: https://gist.github.com/liamrice18/2aae2ec72bdfa34ea308519d131c47b8

Thank you
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liam Rice wrote: however i dont know how to write the program to tell it to use the GBPtoUSD double variable whenever the ExchangeRateFrom input is empty.



Have you learned conditions yet?

Basically, if the get text string is blank, then branch to use a different value. Otherwise, parse the text.

[edit] Well, obviously, you did learned conditionals, as you used it later in your application. You just need to now make the parsing of the double value conditional too.

Henry
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your expected output when the user leaves that field as blank ?
 
Liam Rice
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:What is your expected output when the user leaves that field as blank ?



I want the program to use the "double GBPtoUSD = 1.288;" variable when the user leaves the field blank. Whereas if they enter a value then the program will use the entered value rather than "GBPtoUSD".
 
Liam Rice
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Have you learned conditions yet?

Basically, if the get text string is blank, then branch to use a different value. Otherwise, parse the text.

[edit] Well, obviously, you did learned conditionals, as you used it later in your application. You just need to now make the parsing of the double value conditional too.

Henry



right, how would i do this?

 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liam Rice wrote:I want the program to use the "double GBPtoUSD = 1.288;" variable when the user leaves the field blank...


Awesome !
So, you mean to say that the multiplier should be 1 when the user leaves the field as blank ?
If ExchangeRateFrom.getText() is blank, don't do double.parse double, instead set GetExchange to 1 ?

Btw, I noticed that your if and else statements have the same condition: CurrencyTop.getSelectedItem().equals("USD")
is that what you want ?
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Welcome to coderanch
 
Liam Rice
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:

Liam Rice wrote:I want the program to use the "double GBPtoUSD = 1.288;" variable when the user leaves the field blank...


Awesome !
So, you mean to say that the multiplier should be 1 when the user leaves the field as blank ?
If ExchangeRateFrom.getText() is blank, don't do double.parse double, instead set GetExchange to 1 ?

Btw, I noticed that your if and else statements have the same condition: CurrencyTop.getSelectedItem().equals("USD")
is that what you want ?



Thats a great idea! thanks for getting back to me. Ill try that out now
 
Sheriff
Posts: 67745
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, the Java naming conventions are important. Class names should start with an uppercase letter, and variable names with lowercase. You've kinda got that topsy-turvy.
 
Liam Rice
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:By the way, the Java naming conventions are important. Class names should start with an uppercase letter, and variable names with lowercase. You've kinda got hat topsy-turvy.



thanks for the tip ill take that onboard for future, i still cant seem to get this to work. i may have to redesign my program
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i still cant seem to get this to work.


Can you provide more details?
reply
    Bookmark Topic Watch Topic
  • New Topic