Results 1 to 7 of 7
  1. #1
    jamesov89 is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default 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?

  2. #2
    jamesov89 is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    Also, if I am doing math with doubles, do I need to attach a decimal point and zero at the end of every number?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Shouldn't this:
    Java Code:
    Scanner in; new Scanner(System.in);
    be:
    Java Code:
    Scanner in = new Scanner(System.in);
    ?

  4. #4
    jamesov89 is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    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\:>

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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)

  6. #6
    jamesov89 is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    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.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

    Java Code:
    javac tempconv.java
    It only compile your code, not run. To run the code you have to use java command.

    Java Code:
    java tempconv

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •