Hi guys,
I am working on an assignment for a class of mine (Java) and I am really frustrated with it.

This was my very last attempt (out of like 20)...

    System.out.print( "Enter temperature in Celsius to convert to Fahrenheit: " );
double c = kbinput.nextInt();

double f = ( 9.0/5)*c + 32;

System.out.println( "The temperature in Fahrenheit is: " + f + " degrees. " );

I don't get any specific errors from his "grading compiler"... it only says possible errors (1) found...

That code (with a Scanner declaration for kbinput, aqnd placed inside a method) compiles without any errors or warning in NetBeans, and gives the right answers when executed. Maybe the problem is not in that code but caused by something in the rest of the class?

Edited 2 Years Ago by JamesCherrill

Comments
:D

That code (with a Scanner declaration for kbinput, aqnd placed inside a method) compiles without any errors or warning in NetBeans, and gives the right answers when executed. Maybe the problem is not in that code but caused by something in the rest of the class?

Oh, i see. I suspected that kbinput may not work... What do you recommend as the best approach in solving this problem?

This is my latest attempt:

float temperature;
Scanner in = new Scanner(System.in);      

System.out.println("Enter temp. in Celcius");
temperature = in.nextInt();

temperature = ( 9.0/5)*temperature + 32;

System.out.println("Temperatue in Fahrenheit = " + temperature);

Also, in the error log, it says that it keeps on finding doubles... i don't see how it is possible because i only used floats within this code? It also says it can't find the symbols for line 2, Scanner in = new Scanner(System.in);

Edited 2 Years Ago by <M/>

You have to import java.util.Scanner; to use the class name "Scanner" without fully-qualifying the name.

The constant 9.0 is a double value (that's how the language is defined), so the calculation is done in double, with a theoretical possible loss of precision when you assign that result to a float. If this bothers you either make all your variables doubles, or change your constants to float (eg 9.0f).

Apart from that there's nothing wrong with your code.

Edited 2 Years Ago by JamesCherrill

Comments
thanks :D

Well, i may have to email the professor on why i am getting these errors then.

I do agree, i don't see much possible errors but there could be an error with how he set up the program that grades our snippets of code...

Okay, i still get errors but not i have this:

import java.util.Scanner;
public class Exercise2_1 {
public static void main(String[] args) { 

        float temperature;
    Scanner in = new Scanner(System.in);      

    System.out.println("Enter temp. in Celcius");
    temperature = in.nextInt();

    temperature = (9.0/5.0)*temperature+32;

    System.out.println("Temperatue in Fahrenheit = " + temperature);

} 
} 

This is one of the errors: required: float, found: double, 1 error
And here is another: temperature = (9.0/5.0)*temperature+32; has no precision

Edited 2 Years Ago by <M/>

Did you read my previous post? I explained why you get precision-related errors, and how to fix them, but you seem to have ignored it.

Comments
sorry

Did you read my previous post? I explained why you get precision-related errors, and how to fix them, but you seem to have ignored it.

Sorry about that, I seem to have the missed the point on what you have meant.

In Java when you enter a floating point literal like 9.0 the compiler creates a double value, not a float. That's where ypur doubles are coming from. So when you then assign that value to a float you have a possible loss of precision - hence the warnings. Either make your literals floats (eg 9.0f) or, better, make your variables doubles.

So i changed the code up a little bit, it is better but there are still errors.

Any ideas what is going on now?

import java.util.Scanner;

public class ctof{

     public static void main(String[]args){
        System.out.println("Enter a temperature in Celsius: ");
        Scanner input = new Scanner(System.in);
    double c = input.nextDouble();
    double f = ((9.0/5.0)*(c + 32));
        System.out.println("Fahrenheit: " + f);
     }
}

The calculation from Celsius to Fahrenheit is incorrect.

First multiply the temperature (C) by 1.8 and then add 32.

The correct calcuation would be (1.8*c)+32

(9.0/5.0) would be 1.8

@JamesCherrill- According to my teacher's program, this is the problem:

First Difference Occurs at: byte 33, line 1
On Line 1 its column 33
Line 1 Wrong: Enter a temperature in Celsius: 
Line 1 Right: Enter a temperature in Celsius: The temperature is 397.40000000000003 in Fahrenheit. 

sdiff side by side difference your output versus solution....
Enter a temperature in Celsius:                   | Enter a temperature in Celsius: The temperature is 397.400000
Fahrenheit: 144.77777777777777                    <

Octal Dump Difference
0000040  \n   F   a   h   r   e   n   h   e   i   t   :       | 0000040   T   h   e       t   e   m   p   e   r   a   t   u  
0000060   .   7   7   7   7   7   7   7   7   7   7   7   7   | 0000060   i   s       3   9   7   .   4   0   0   0   0   0  

Octal Dump Your output...
0000000   E   n   t   e   r       a       t   e   m   p   e   r   a   t
0000020   u   r   e       i   n       C   e   l   s   i   u   s   :    
0000040  \n   F   a   h   r   e   n   h   e   i   t   :       1   4   4
0000060   .   7   7   7   7   7   7   7   7   7   7   7   7   7   7  \n
0000100

Octal Dump Solution..
0000000   E   n   t   e   r       a       t   e   m   p   e   r   a   t
0000020   u   r   e       i   n       C   e   l   s   i   u   s   :    
0000040   T   h   e       t   e   m   p   e   r   a   t   u   r   e    
0000060   i   s       3   9   7   .   4   0   0   0   0   0   0   0   0
0000100   0   0   0   0   3       i   n       F   a   h   r   e   n   h
0000120   e   i   t   .  \n
0000125

@kal crazy- i get the same errors.... :(

That is totally ridiculous. The "error" seems to be that your output is on 2 lines instead of one (although two lines is better IMHO). What is the celsius temp that was input? - the output doesn't show it.

ps: You must read kal_crazy's post. It's essential.

@James, i don't know what the input is, it only shows you the result of what was inputted... :( I did read kal's post and i understand what he and you mean but for some odd reason, i just don't understand my own code :(

import java.util.Scanner;
public class Exercise2_1 {
public static void main(String[] args) {
        System.out.println("Enter a temperature in Celsius: ");
        Scanner input = new Scanner(System.in);
    double celsius = input.nextDouble();
    double fahrenheit = (1.8*celsius) + 32;
        System.out.println("Fahrenheit: " + fahrenheit);
}
}

This was my latest attempt... I kind of feel upset due to the fact that i can't figure this out, espicially with all the time i have spent with java... :(

Changing the println on line 4 to a print will eliminate the newline that your automated test dislikes.

I have no idea how you are expected to get the right result if you haven't been told the input value that was used.

One thing that's incorrect is your output. It should not be "Fahrenheit: XX.XX". It should be in the format "The temperature is XX.XX in Fahrenheit."
where XX.XX is just the floating point number.

@james, i have it set up like that now (still is an error).

@Mouche, you mean like this:

import java.util.Scanner;
public class Exercise2_1 {
public static void main(String[] args) {
        System.out.print("Enter a temperature in Celsius: ");
        Scanner input = new Scanner(System.in);
    double celsius = input.nextDouble();
    double fahrenheit = (1.8*celsius) + 32;
        System.out.println("The temperature is: " + fahrenheit + " in Fahrenheit.");
}
}

but how would i make it a floating point number? (guessing...) where do I put "F" in order to make it a floating point?

Okay so good news you guys :D I figured it out! Apparently the teacher had an error on his part and said my code is fine :D

Thanks guys, more questions are yet to come :D

This question has already been answered. Start a new discussion instead.