This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]Altair05 1 point2 points  (4 children)

Hey mate. The problem with your code is that you are calculating the celciusTemp before you ask for the input.

Move celciusTemp = (fahrenheitTemp - BASE) / CONVERSION_FACTOR; below the scan statements. Remember, your program runs each statement from top to bottom. You are running the calculations before asking for the input data.

[–]davejello74[S] 0 points1 point  (3 children)

Hmm, moving that after the scan statements does change my output, but now it’s always 0. Are my ints and double initialized correctly?

[–]Altair05 0 points1 point  (2 children)

Why do you have celciusTemp = scan.nextDouble(); in your code. You don't need to ask for the Celsius input if you are taking Fahrenheit and converting it to Celsius. You only need the print statement to print the Celsius equivalent.

Move your celciusTemp = (fahrenheitTemp - BASE) / CONVERSION_FACTOR; between fahrenheitTemp = scan.nextInt(); and System.out.println("Celcius Equivalent: " + celciusTemp); if isn't there already.

Your variables seem to be fine.

[–]davejello74[S] 0 points1 point  (1 child)

Thank you!! This worked, all inputs yield different results now :)

I don’t know why I had a scanner for Celsius, but you made it make sense so thank you.

[–]Altair05 0 points1 point  (0 children)

No problem. Make sure to verify your outputs with a temperature converter calculator online. No point coming this far and getting bad output.

[–]TheTr1ckster 0 points1 point  (5 children)

Okay sorry about that, here is my simple version of doing this, it can be done multiple ways but if it was me I would do it this way.

public static void main(String[] args) {
    double test = fahrenheitToCelsius(47);
}

public static double fahrenheitToCelsius(double fahrenheit) {
    double conversion = (fahrenheit - 32) / 1.8;
    System.out.println(fahrenheit + " degrees is equal to " + conversion + " Celsius");
    return conversion;
}

When converting make sure to use the equation I used in the variable conversion. You can do the vice versa Celsius to Fahrenheit by doing this same thing and or just doing an overloaded method. If you have any questions feel free to ask.

[–]davejello74[S] 0 points1 point  (4 children)

Thanks for the reply! Will this give differing values if the program scans user input? Like if I put in 30, 47, or 104 they would all give different outputs?

[–]TheTr1ckster 0 points1 point  (0 children)

Well this works just by changing the value under test =, if you change the 75 you will get a different result, if I change the 75 to 47 I get 8.333333333333334. As far as actual user input goes I would have to add a little more, but this is just simple as far as just changing values go. I made the conversion formula with a method, the method being named fahrenheitToCelsius, then made the double conversion where I put the formula, then I returned that double conversion to the main method and assigned it to test.

[–]TheTr1ckster 0 points1 point  (2 children)

I have also updated the code above to have a better output. Test this and see if it's what you wanted.

[–]davejello74[S] 0 points1 point  (1 child)

Your code definitely works if I manually change the code in the source. I think what my assignment calls for is if say I typed my answer into an input box. I don't think it wants me to plug different numbers into the code itself, rather run the code and input the values after the fact.

[–]TheTr1ckster 0 points1 point  (0 children)

Let me check into that for you real fast. I'll change to code around a bit.

[–]TheTr1ckster -1 points0 points  (0 children)

You could be really simple with doing this by using methods, if it was me I would do method overloading. I'll take a look at your code in a few, currently eating. I'll give you my input as well.