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!
2 2683
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 - boolean inDegreesC = false;
-
double temperature = 0.0;
-
String temp = JOptionPane.showInputDialog(null, "Enter a temp .....");
-
if( temp.endsWith("f") || temp.endsWith("F")) {
-
inDegreesC = false;
-
temperature = Double.parseDouble(temp.substring(0, temp.length() - 1));
-
}
-
else if(temp.endsWith("c") || temp.endsWith("C")) {
-
temperature = Double.parseDouble(temp.substring(0, temp.length() - 1));
-
inDegreesC = true;
-
}
-
else {
-
temperature = Double.parseDouble(temp);
-
inDegreesC = true;
-
}
-
-
-
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.
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 - boolean inDegreesC = false;
-
double temperature = 0.0;
-
String temp = JOptionPane.showInputDialog(null, "Enter a temp .....");
-
if( temp.endsWith("f") || temp.endsWith("F")) {
-
inDegreesC = false;
-
temperature = Double.parseDouble(temp.substring(0, temp.length() - 1));
-
}
-
else if(temp.endsWith("c") || temp.endsWith("C")) {
-
temperature = Double.parseDouble(temp.substring(0, temp.length() - 1));
-
inDegreesC = true;
-
}
-
else {
-
temperature = Double.parseDouble(temp);
-
inDegreesC = true;
-
}
-
-
-
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 :(
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |