Results 1 to 7 of 7
Thread: Temperature converstion
- 09-29-2008, 01:54 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
Temperature converstion
Assignment:
Write and run a java program that gets an integer temperature form the keyboard and then convert it to degrees C and F, displaying the results a single System.out.println.
Turn in a printed listing of your program and it's output. Run the program 3 times using values 68, 20, and -40.
Sample output:
What is the temperature to convert? 25
25 converts to
-3 deg C
77 deg F
My code:
import java.util.Scanner;
public class tempconv {
public static void main(String[] args) {
double temp; //temperature input
double cel; //Celsius
double fah; //Fahrenheit
Scanner in; new Scanner(System.in);
System.out.println("What is the temperature to covert?");
temp = in.nextDouble();
System.out.println();
cel = (temp - 32) * 5/9;
fah = temp * 9/5 + 32;
System.out.println(temp + "converts to" + cel + "deg C" + fah + "deg F");
}
}
MY ERROR:
tempcov.java:28: variable in might not have been initialized
temp = in.nextDouble();
^
Any Ideas?
- 09-29-2008, 01:56 AM #2
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
Also, if I am doing math with doubles, do I need to attach a decimal point and zero at the end of every number?
-
Shouldn't this:
be:Java Code:Scanner in; new Scanner(System.in);
?Java Code:Scanner in = new Scanner(System.in);
- 09-29-2008, 02:06 AM #4
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
Thanks, just a slight overlook I suppose.
Now I have a different problem: When I run the program it just forwarded me back to my original directory.
F\:>javac tempconv.java
F\:>
-
um,... that's not running it. That's compiling it. You need to check out the getting started section of the Java tutorials: Trail: Getting Started (The Java™ Tutorials)
- 09-29-2008, 02:39 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
I read those but even so, I am unsure as to what needs to be changed. I did not read that error in the tutorial.
- 09-29-2008, 04:51 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What error are you talking about?
You haven't read that link send to you by Fubarable. If you have read it, all what you need is there.
After writing your code, have to compile it first. javac command do it for you. According to your application it should be like this.
It only compile your code, not run. To run the code you have to use java command.Java Code:javac tempconv.java
Java Code:java tempconv


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks