• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java GUI Calculation Help  RSS feed

 
Hank Harrison
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I have this gui built but i'm in the last steps and i can't figure out how to do this last calculation. I want the user to input and odometer (in feet) and have it use that and the Duration (in hours) to calculate speed in feet per second. Ive been trying to reference it using various methods but I'm stuck. I have it set as a string but whenever i try to pretty much use somewhat the same formula i did for total time it just gives me errors. How do I use that number the user inputs into a formula with the output of the Duration? Which method should i use? thanks :-)

 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget all about the GUI and do the calculations in a little class of their own.
You probably want a method like thisI am not 100% sure my formula is accurate

Are you aware of this enum which has methods for converting hours to seconds, etc?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hank Harrison wrote:Ok so I have this gui built but i'm in the last steps and i can't figure out how to do this last calculation. I want the user to input and odometer (in feet) and have it use that and the Duration (in hours) to calculate speed in feet per second. Ive been trying to reference it using various methods but I'm stuck.

OK, well first off, I'm no GUI expert, so plugging it into your GUI is up to you, but what I DO know is that this calculation almost certainly has nothing whatsoever to do with your GUI, so my advice would be to set up a separate class to do it. And that class should NOT have a single line in it that defines or references a 'J' class (JFrame, JPanel etc.).

I have it set as a string but whenever i try to pretty much use somewhat the same formula i did for total time it just gives me errors.

Well, that might be your problem right there. Strings are NOT numbers. Have a look at the StringsAreBad page for more details.

How do I use that number the user inputs into a formula with the output of the Duration? Which method should i use? thanks :-)

Well, that'll depend on what kind of number you need; but just one might be Double.valueOf(String).

But whatever you do, you must convert that String to a number - and that conversion must allow for the fact that your user is a complete idiot and might enter "jsdhfg.0" instead of a proper number.

But the main point is: A distance calculation is a distance calculation, and it can be used anywhere. So don't define it directly in your GUI.

Winston

[Edit] Too slow.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hank Harrison wrote:Ok so I have this gui built...

And it has incredibly LONG LINES, which makes the whole thread very hard to read.
I'd break them up myself, but you have gazillions of them, so I suggest you edit the post yourself after reading the link.

Winston
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . LONG LINES . . . gazillions of them, so I suggest you edit the post yourself after reading the link.

Winston
No, there were only a few million.

Non‑mods can no longer edit such posts, so I edited it. I simply deleted a few hundred lines which were not relevant to the speed calculation and added a few newlines.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
[Edit] Too slow.


Actually what you said was rather different from what I said, so what does it matter who got there first?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!