• Post Reply Bookmark Topic Watch Topic
  • New Topic

number format exception  RSS feed

 
Liam Rice
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to coderanch
 
Liam Rice
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i still cant seem to get this to work.

Can you provide more details?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!