Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion table

 
Mark Anders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, im trying to produce a simple coversion table, using and input field, button and output field to convert pounds to us dollars. Could anyone help me out with a simple bit of code to perform the action? cheers
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess we could help if you told us what your problem is. What code do you have already?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want a fancy GUI or not?

For simlicity, you can use a simple JPanel with 2 JTextFields and 1 JButton as your GUI layout.

Nick
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be more specific? What exactly do you need help with? What problems have you encountered in trying to write this on your own? Are you having problems with the GUI itself? Or do you need help with the conversion calculation? If you can specificly describe what you are struggling with, we will be able to help you. However, as it is, we can only guess how to help, and the chances are that it won't be all that helpful.

Layne
 
Mark Anders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes ive got the gui sorted with a jpanel, two text fields and a button.
Its just the code im struggling with. Basically the user will put in a value for in pounds, and then when the button is clicked, the program needs to multiply the number by the exchange rate then output the answer in us dollars in to the second text field. fairly simple but im just having a few problems with the code. Many thanks all
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a action listener to the Button. In the listener code get the value's you typed in the GUI & pass on to a method that has the business logic , Show the return value from the method in the GUI.
 
Mark Anders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right i undersand that but how would i structure it? Ive done previous excercises where for example, i produced a insurance quote calculator where inputs where egine size, and age, and using if statements, the program outputted one of four set prices for the quote. But in this instance, im unsure of how to start, and do i need to use if statements?
cheers
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Anders:
Right i undersand that but how would i structure it? Ive done previous excercises where for example, i produced a insurance quote calculator where inputs where egine size, and age, and using if statements, the program outputted one of four set prices for the quote. But in this instance, im unsure of how to start, and do i need to use if statements?


Let's say that, instead of having to program a computer to do this job, you had to tell some poor intern at your company how to it.

In other words, someone would give your intern a slip of paper with an amount (in pounds) written on it. The intern has to figure out what that amount would be in US dollars, and write it on another slip of paper. The intern has a good calculator sitting on the desk, so any kind of math will be no problem.

So how would you instruct the intern? How would you do the job, if you were the intern?

Figure that out, and you've pretty much got your program figured out.

- Jeff
 
Mark Anders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers, i know what needs to be done, i'm just fairly new to the code and am unsure of how to correctly get the input from one text field and then output it to the next. for the sake of argument il say that the exchange rate is �1 : 1.5$. Could you give me any pointers on the code? cheers
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Anders:
cheers, i know what needs to be done, i'm just fairly new to the code and am unsure of how to correctly get the input from one text field and then output it to the next. for the sake of argument il say that the exchange rate is �1 : 1.5$. Could you give me any pointers on the code? cheers


Hmmmm... I thought you had just said that you had done that sort of thing before (the inputting and outputting), and that you needed hints with the logic of the program (whether to use if statements to get the result or not, etc.)

Really, you should post what you have so far, and tell us exactly what specifc problems you're having. Are you unable to get it to compile? Does it compile but just not work correctly? Is the wrong amount output? Is anything output?

I don't think any of us here are just going to give you the solution. Apart from the ethical implications, we all happen to think it's fun beating our heads against a wall and then finally figuring it all out. We don't want to deprive you of that!

That said, here are two tips:

First, break it down into the two main problems, and try to solve them separately. One problem is reading the value from a text box, and then putting the result of some calculation in another text box, based on user input. Get to the point where you can do that. What the actual values are, and what the calculation is, is completely irrelevant. In fact, your first goal should be to just put the contents of the input field right into the output field as it is. That would be enough to get the mechanics of it down. You'll probably want to take a look at the official documentation for the TextField class.

Second, get the conversion part working. You don't need a fancy interface for that--it can all be done on the console with regular old System.out.println() calls--so you could do this part first, if you wanted.

- Jeff
 
Mark Anders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm i realise that was quite good advise not giving me the answer!
Ive had a play around,
got a jframeform with absolute layout;
Input jtextfield called txtInput
Output jtextfield called txtOutput
Convert jbutton called cmdConvert

Ok ive managed to get it working, ive used an exchange rate of �1 : $2, as for a reason unknown to me, when i tried 1.5 i had an error?
And so now i can input a value, and when the button is clicked it effectively doubles the amount ive inputted nd outputs it to the second textfield. hooray! heres the code for the button:

private void cmdConvertActionPerformed(java.awt.event.ActionEvent evt) {

txtOutput.setText("");

if (Integer.parseInt(txtInput.getText()) > 0)

rate = 2;

else
rate = 0;

result = rate * (Integer.parseInt(txtInput.getText()));

txtOutput.setText('$' + Float.toString(result));

And of course ive stated the variables "result" and "rate" at the beginning of the program.

So could you suggest to me why it wouldnt allow a decimal for the value of rate?

And also, would it be fairly simple again to have a textfield where the user can input the rate? cheers Jeff
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Anders:
[QB]And of course ive stated the variables "result" and "rate" at the beginning of the program.

So could you suggest to me why it wouldnt allow a decimal for the value of rate?


What type did you declare for these variables?
 
Mark Anders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
float rate;
float result;
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

When you tried 1.5, was the error something like: "Type mismatch - can't convert from double to float?"

This is because decimal literals are by default doubles. You can put an 'f' after the number to tell the compiler that you want it to be a float.

E.g. float rate = 1.5f

As for adding a rate input field, you can do the same as the amount one. Except you need to parse out a float rather than an int.
 
Mark Anders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok this is what ive done;

txtOutput.setText(""); //clears field

if (Integer.parseInt(txtRate.getText()) == 0) //doessnt work - // should be if
//txtRate = 0, then
//use 1.5 as rate
rate = 1.5f;

else
rate = (Integer.parseInt(txtRate.getText())); //this works but
// doesnt allow
//decimals??

result = rate * (Integer.parseInt(txtInput.getText()));

txtOutput.setText('$' + Float.toString(result));

When i just used a fixed rate and added an f to the rate it worked, but now when i input the rate on the program interface, it doesnt allow decimals. Also, if a rate isnt entered id like the rate to be a default of 1.5 but i cant get that to work either. any pointers? cheers
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Horatio Westock:

As for adding a rate input field, you can do the same as the amount one. Except you need to parse out a float rather than an int.


Yup. And, for that matter, you'll probably want to parse your amount input as a float too. Otherwise, the user won't be able to enter anything other than whole pounds amounts. (Well, they'll be able to enter them, but, well, try it and see what happens...)

But it looks like you're on the right track. Congrats!

Here are two nitpicky things that are more about style. You might find it inseresting to ponder them, but you can probably consider them optional:

First, take a look at this line:

txtOutput.setText("");

What would happen if you got rid of it?

Second, notice that you're always going to be calling parseInt() at two different spots to convert the text of your input field to a numeric value. That parseInt() method is running its own code, and using processor cycles everytime it's called. Sure, we're talking about milliseconds here, but it still seems like kind of a shame to do the same exact thing twice, doesn't it? Is there a way to only have to call that parsing method once?

(Bonus nitpick: If the user enters zero, do you actually need to go through with calculating the conversion?)

- Jeff
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Anders:
rate = (Integer.parseInt(txtRate.getText())); //this works but
// doesnt allow
//decimals??


OK. What do we know about Integers as apposed to floating point types? Have a look at some of the other classes in java.lang and see if there is anything interesting there

Edit: Not quick enough. And everything Jeff says above!
[ April 04, 2005: Message edited by: Horatio Westock ]
 
Mark Anders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, got rid of that line!!
as for what you said about the repeated parsing, im not sure how i could eliminate that.
And how would i go about allowing input of decimal values for the exchange rate and possibly outputting a decimal too, say two decimal places? cheers
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Anders:

as for what you said about the repeated parsing, im not sure how i could eliminate that.


Think about how you could parse it once, store it somewhere, then use the stored value.

[QB}
And how would i go about allowing input of decimal values for the exchange rate[/QB]


Both Jeff's and my post hint at this. Instead of doing a parse Integer, you probably want to be parsing a float. Have a look at the java.lang package and see if there is a class (similar to the Integer class) that might have a method for doing this.


and possibly outputting a decimal too, say two decimal places? cheers


Once you have a float, you can output it directly, though if you want specific formatting, then you might want to read the API for a class like DecimalFormat.
 
Mark Anders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see now, ok thanks a lot all. Im off on holiday in the morning so il have a crack during the week and see what i can get done, and il post back next week. Thankyou all youve been very helpful! Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic