• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Java GUI Calculation Help

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)

 
Marshal
Posts: 80736
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80736
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80736
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic