aspose file tools*
The moose likes Beginning Java and the fly likes Conversion table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Conversion table " Watch "Conversion table " New topic
Author

Conversion table

Mark Anders
Greenhorn

Joined: Apr 03, 2005
Posts: 9
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

Joined: Jul 11, 2001
Posts: 14112
I guess we could help if you told us what your problem is. What code do you have already?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Mark Anders
Greenhorn

Joined: Apr 03, 2005
Posts: 9
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

Joined: Sep 28, 2004
Posts: 1228
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.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Mark Anders
Greenhorn

Joined: Apr 03, 2005
Posts: 9
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

Joined: Mar 29, 2005
Posts: 71
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

Joined: Apr 03, 2005
Posts: 9
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

Joined: Mar 29, 2005
Posts: 71
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

Joined: Apr 03, 2005
Posts: 9
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

Joined: Feb 23, 2005
Posts: 221
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

Joined: Apr 03, 2005
Posts: 9
float rate;
float result;
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
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

Joined: Apr 03, 2005
Posts: 9
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

Joined: Mar 29, 2005
Posts: 71
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

Joined: Feb 23, 2005
Posts: 221
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

Joined: Apr 03, 2005
Posts: 9
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

Joined: Feb 23, 2005
Posts: 221
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

Joined: Apr 03, 2005
Posts: 9
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
 
 
subject: Conversion table