Results 1 to 9 of 9
  1. #1
    king4ever is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default My code for converting temperatures...

    Hello All,

    I just stayed around 6 hours to try and figure out how to write a code to convert Fahrenheit to Celsius. Here's my code:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class fahrenheitcelsius {
    public static void main (String[]args)
    throws IOException
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the Temperature in Fehrnheit");
    String degree;
    degree = br.readLine();
    int c = Integer.parseInt(degree);
    c = (int) ((c-32)*(5)/(9));
    System.out.println("The Temprature in Fahrenheit is \n"+c);
    }
    }

    I wanna know if this is considered good for someone that doesn't have any programming background. Also I wanted to know If I can use an IF statement to make the program not only convert from Fahrenheit to Celsius but vice-versa. Anyways here's what I had in mind:

    if(br.readLine() == C){
    System.out.println("Enter the Temperature in Celsius");
    String degree1;
    degree1 = br.readLine();
    int f = Integer.parseInt(degree1);
    f = (int) (( f -32)*(0.55))
    System.out.println("The Temperature in Celsius is \n" +f);
    }else{
    System.out.println("Enter the Temperature in Fahrenheit")
    String degree;
    degree = br.readLine();
    int c = Integer.parseInt(degree);
    c = (int) ((c*(9/5))+32)
    System.out.println("The Temperature in Celsius is\n"+c);

    But I couldn't get that to work , and because it's my first time on java ever I don't know if i'm really on the track or not.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by king4ever View Post
    But I couldn't get that to work
    What really happens here? Errors? Code doesn't compile? Exceptions? Something else?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    king4ever is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    it does execute but it let me enter values two times , and no matter what I wrote it always gives my the else part, it's like the IF doesn't exist.

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Java Code:
    if(br.readLine() == C)
    What is the C above? It's a String, right? Do we use Strings like this? Or we use double quotes " "? Like,
    Java Code:
    if(br.readLine() == "C")

    Better way go for the equals method rather than == when you are comparing the Strings. Something like,

    Java Code:
    if(br.readLine().equals("C"))

    See, what happens now?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by king4ever View Post
    if(br.readLine() == C){
    That same mistake made ever again: Strings can't be compared with the == operator, use the String.equals("C") method call instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    king4ever is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks Goldest for your help,

    All it took is to change the == to equals , and put double quotes on the C.

    well, I guess Java is not that bad after all. :p

  7. #7
    king4ever is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    JosAH

    Thanks for your advice, I'll try and remember that next time I'm doing something.

    Regards,

  8. #8
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    Quote Originally Posted by king4ever View Post
    Thanks Goldest for your help,

    All it took is to change the == to equals , and put double quotes on the C.

    well, I guess Java is not that bad after all. :p
    you might also note that Java is one language that does not use "==" to compare strings. This is not so for other languages. For example, in Python, "==" is both used for comparing strings as well as numbers. Java is not that bad, but its not good enough. :)
    Java Code:
    >>> s="string"
    >>> s=="string"
    True
    >>> s=10
    >>> s == 10
    True
    Last edited by JavaHater; 01-18-2011 at 02:50 AM.

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    For those curious, in Java, String is an object that is treated in some ways like a primitive, but it is still an object. == Compares the value of a primitive, or the memory location of an object. For instance:
    Java Code:
    Object a = new Object();
    Object b = new Object();
    System.out.println(a == b); //false
    b = a;
    System.out.println(a == b); //true
    The == doesn't compare the value, but the location - a & b are only equal when they both point to the same location.

Similar Threads

  1. Converting String to Usable code
    By xael in forum New To Java
    Replies: 4
    Last Post: 09-21-2010, 10:08 PM
  2. When converting code into an iPhone app...
    By Gio D. in forum New To Java
    Replies: 0
    Last Post: 03-16-2010, 08:36 PM
  3. converting image to binary code n vice versa
    By nupurashi in forum Advanced Java
    Replies: 4
    Last Post: 04-01-2009, 08:27 PM
  4. Converting URL to URI
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 11:15 AM

Posting Permissions

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