473,411 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Need a little help getting started. Trying to figure out how to convert temps...

I am a java beginner and am trying to tackle a temperature conversion problem but I don't exactly know how to begin. The problem asks that I write a program that allows the user to conver either from degrees Celsius to Fahrenheit or from degrees Fahrenheit to Celsius. I have the following formulas to accomplish this already:
degreesC = 5(degreesF - 32)/9
degreesF = (9(degreesC)/5)+32

Then it asks to prompt the use to enter a temperature and either a C, c, F, or f while ignoring case(im guessing I incorporate ignore.case for this). If other than one of those are entered print an error message and ask to reenter a valid selection. (this i know how to do i think) Then convert the user entered temp to fahrenheit if celsius is entered or vice versa and ask teh user to press Q or q to quit or press any other key to repeat the loop and do another conversion.

My main problem with this problem is just getting started. How should I set up my variables in order to prompt the user to enter a temperature in number format followed by a letter? If anyone could just give me a push in the write direction I would be much obliged. Thanks!
Nov 2 '06 #1
2 2683
r035198x
13,262 8TB
I am a java beginner and am trying to tackle a temperature conversion problem but I don't exactly know how to begin. The problem asks that I write a program that allows the user to conver either from degrees Celsius to Fahrenheit or from degrees Fahrenheit to Celsius. I have the following formulas to accomplish this already:
degreesC = 5(degreesF - 32)/9
degreesF = (9(degreesC)/5)+32

Then it asks to prompt the use to enter a temperature and either a C, c, F, or f while ignoring case(im guessing I incorporate ignore.case for this). If other than one of those are entered print an error message and ask to reenter a valid selection. (this i know how to do i think) Then convert the user entered temp to fahrenheit if celsius is entered or vice versa and ask teh user to press Q or q to quit or press any other key to repeat the loop and do another conversion.

My main problem with this problem is just getting started. How should I set up my variables in order to prompt the user to enter a temperature in number format followed by a letter? If anyone could just give me a push in the write direction I would be much obliged. Thanks!
All that time and you still had not started?
For starters, use JOptionPane.showInputDialog which asks the user to input their temperature followed by c for degreesC, f for degreesF and tell them that not putting in any units would mean the temp is in degreesC(I think I have posted this before).
your code would go like

Expand|Select|Wrap|Line Numbers
  1. boolean inDegreesC = false;
  2. double temperature = 0.0;
  3. String temp = JOptionPane.showInputDialog(null, "Enter a temp .....");
  4. if( temp.endsWith("f") || temp.endsWith("F")) {
  5.     inDegreesC = false;
  6.     temperature = Double.parseDouble(temp.substring(0, temp.length() - 1));
  7. }
  8. else if(temp.endsWith("c") || temp.endsWith("C")) {
  9.      temperature = Double.parseDouble(temp.substring(0, temp.length() - 1));
  10.     inDegreesC = true; 
  11. }
  12. else {
  13.      temperature = Double.parseDouble(temp);
  14.     inDegreesC = true; 
  15. }
  16.  
  17.  
  18.  
After this inDegreesC will tell you whether or not the temperature entered is in degreesC and you can now play around with it as you please.
Nov 2 '06 #2
All that time and you still had not started?
For starters, use JOptionPane.showInputDialog which asks the user to input their temperature followed by c for degreesC, f for degreesF and tell them that not putting in any units would mean the temp is in degreesC(I think I have posted this before).
your code would go like

Expand|Select|Wrap|Line Numbers
  1. boolean inDegreesC = false;
  2. double temperature = 0.0;
  3. String temp = JOptionPane.showInputDialog(null, "Enter a temp .....");
  4. if( temp.endsWith("f") || temp.endsWith("F")) {
  5.     inDegreesC = false;
  6.     temperature = Double.parseDouble(temp.substring(0, temp.length() - 1));
  7. }
  8. else if(temp.endsWith("c") || temp.endsWith("C")) {
  9.      temperature = Double.parseDouble(temp.substring(0, temp.length() - 1));
  10.     inDegreesC = true; 
  11. }
  12. else {
  13.      temperature = Double.parseDouble(temp);
  14.     inDegreesC = true; 
  15. }
  16.  
  17.  
  18.  
After this inDegreesC will tell you whether or not the temperature entered is in degreesC and you can now play around with it as you please.
I've never used JOptionPane.showInputDialog or any of the parse at all...my book has all of that listed in the "optional" section of each chapter. I think i'm way in over my head with this java stuff but thank you for this. I'll try and feel my way around in the dark and see if I can't come up with something resembling a program. If you saw the code I had been working on for this I bet you would laugh :(
Nov 2 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Benji99 | last post by:
Hi guys, I'm starting to learn Python and so far am very impressed with it's possibilities. I do however need some help with certain things I'm trying to do which as of yet haven't managed to find...
4
by: Marquisha | last post by:
If this is off-topic, please forgive me. But I thought this might be the perfect spot to get some advice about how to proceed with a project. Working on a Web site design for a nonprofit...
5
by: iminal | last post by:
I am trying to make a very simple program and am very new to the whole programming thing. my program is supposed to ask a user for any time in the for format XX:XX:XX and then ask for a time...
26
by: Michael McGarry | last post by:
Hi, I am pretty sure this is not possible, but maybe somehow it is. Given a variable, can I tell what type it is at runtime? Michael
2
by: Cider123 | last post by:
Trying to figure out how to replicate the same interface as what a ListView control has. I have everything working for the most part, with exception of 1 minor setback. I can't seem to pass...
31
by: Martin Jørgensen | last post by:
Hi, I've had a introductory C++ course in the spring and haven't programmed in C++ for a couple of months now (but I have been programmed in C since january). So I decided to do my conversion...
9
by: whitgurley | last post by:
I've searched the web as well as I can for a solution to this problem but have found nothing and just don't know enough about JavaScript to figure out what's going. What I'm trying to do is convert...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
1
by: Dancefire | last post by:
Hi, everyone, I'm trying to use std::codecvt<to do the encoding conversion. I am using following code for encoding conversion between wchar_t string and char string(MBCS). I am not sure am I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.