• Post Reply Bookmark Topic Watch Topic
  • New Topic

currency converter not working  RSS feed

 
Liam Rice
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone! i'm currently close to finishing my first ever java project however i have come across an error and i'm hoping someone can help me...
I'm making a currency converter and i want to be able to choose to convert any of the below currencies from EURO (EUR), however, since I've added two combo boxes this doesn't seem to be working, can anyone see where i have gone wrong?
also, this code may look shocking to most of you so i'd also like to ask if anyone knows an easier way of doing this code repetitivity?



Thanks in advance! any feedback is greatly appreciated. have a great day
 
Liam Rice
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys i have just noticed i have the wrong variable names in the double variables up top, i changed this when i copied it over to here because i now know its bad practice to have a variable start with a capital letter. However, i forgot to change the variable names for the rest of the code so just ignore that please thank you!
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Define "not working". We are not mind readers. We don't know what is happening.

2. What debugging have you done? Did you add System.out.println(...) statements in your code to see what part of your code is executing.

3. Using multiple nested if statement like that is not a very good design. A different approach might the to store the currency and the conversion rate in a HashMap:


Then in your ActionListener you just get the selected item and retrieve the conversion rate from the HashMap:
 
Liam Rice
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:1. Define "not working". We are not mind readers. We don't know what is happening.

well, the setText doesnt run when both the comboBox values are met. For example, i select EUR from cmbCurrency and INR from cmbCurrencyTo and when these two are selected the text field 'txtExchange' is meant to set text to 'double EURtoINR = 71.39;'

Rob Camick wrote:2. What debugging have you done? Did you add System.out.println(...) statements in your code to see what part of your code is executing.

No, im a beginner, ive never done debugging in my life. still need to learn how to.

Rob Camick wrote:3. Using multiple nested if statement like that is not a very good design. A different approach might the to store the currency and the conversion rate in a HashMap:

thanks for this, just what i was looking for when i asked for an easier way of doing the code repetitivity

Rob Camick wrote:Then in your ActionListener you just get the selected item and retrieve the conversion rate from the HashMap:


thanks, ill try this
 
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
Liam Rice wrote:
Rob Camick wrote:2. What debugging have you done? Did you add System.out.println(...) statements in your code to see what part of your code is executing.

No, im a beginner, ive never done debugging in my life. still need to learn how to.

It's more of an art than a science.  Start by putting System.out.println() (SOP) statements at the beginning of methods you think are being called.  Then add SOPs where you are making assumptions about the state of the methods.  Print out values of variables and see if they what you suppose.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:. . . . A different approach might the to store the currency and the conversion rate in a HashMap . . .
I might have used the doubles “naked” and allow the compiler to organise boxing conversion rather than new Double(...).
An alternative would be to use a Currency enum. Read about enums in the Java™ Tutorials and the Java® Language Specification (=JLS). Beware: the JLS can be difficult to understand; that is one of the easier parts to understand, I think.
Unfortunately both those approaches have the problem that it is awkward to change currency rates; exchange rates can change significantly minute to minute or second to second.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!