Please any one can help me to fix my last statement of code I'm traying to have a user input the temp and then convert ot either F or C. But my last if else statement doesnot engage well with the over all code. I'm beginner Java programnig. Thank you in advanced.

import java.util.*;
public class CelsiusFahrenheit {


    public static void main(String[] args) {

        Scanner in = new Scanner(System.in); 
        System.out.println("Would you like to convert from Fahrenheit to Celcius or Celcius to Fahrenheit?");
        System.out.println("(1) Fahrenheit to Celcius");
        System.out.println("(2) Celcius to Fahrenheit");
        int choice = in.nextInt(); //Get user input on whether to do F to C or C to F
        if (choice != 1 && choice != 2)

        {
            System.out.println("That was not one of the choices."); //handle if they don't enter a valid choice.
            }
            else if (choice == 1) //Fahrenheit to Celcius
            {
            System.out.println("Please enter the temperature in Fahrenheit to be converted to Celcius:");
            double toConvertFahrenheit = in.nextDouble(); //Get user input for value to convert... Used double to support decimals
            toConvertFahrenheit = (((toConvertFahrenheit-32)*5)/9); //subtract 32, multiply by 5, then divide by 9
            System.out.println("Your converted value is: " + toConvertFahrenheit + " degrees Celcius.");
            System.out.println("You Need to Wear a t-shirt");
            }
            else if (choice == 2)
            {
            System.out.println("Please enter the temperature in Fahrenheit to be converted to Celcius:");
            double toConvertCelcius = in.nextDouble();
            toConvertCelcius = (((toConvertCelcius*9)/5)+32); //multiply by 9, divide by 5, add 32.
            System.out.println("Your converted value is: " + toConvertCelcius + " degrees Fahrenheit.");

            // What to wear statement IS NOT WORKING.
            if (toConvertFahrenheit <= 32) {
                System.out.println("Your must wear a coat" + toConvertCelcius );

            }
            else if (toConvertCelcius >= && toConvertFahrenheit <= 211 ) {
                System.out.println("Your need to wear t-shirt" + toConvertCelcius);
            }

            } // comment 1


    } // comment 2

} // comment 3 

else if (toConvertCelcius >= && toConvertFahrenheit <= 211 )

Your condition after >= must follow by something that can be compared with toConvertCelcius variable. That should give you an error at compile time (syntax error).

Now speaking of logic, you are using if & else-if statements to compute the conversion which is OK.

Currently, your code has a major flaw in variable scope. You need to understand variable scopes. If you declare a variable inside a pair of curly bracket '{..}', your variable will not be recognize outside of the scope and becomes unknown. As a result it will be a compile error.

Another flaw, your comment block is currently inside the Celcius to Fahrenheit conversion only. If you are to convert Fahrenheit to Celcius, you won't see the comment.

To fix this, you need to fix the first flaw before you can do the second. You need to move your variable declaration outside the if/else-if statement. Once you get it done, then you can reuse the "choice" variable again, so you would know which variable you want to check in your condition before displaying a comment.

There are optimal ways to do this, but I would leave it up to you to learn. My suggestion would require not much modification to your current code (simply move around code lines and add a little more here and there).

Hope this help.

Taywin,

I really appreacited your time and value information it helps a lot to.

Regards,
Toldav

Taywin,

Here is my modification would you please check my code if you don't mind.
Thank you.

import java.util.*;
public class CelsiusFahrenheit {


    public static void main(String[] args) {

        Scanner in = new Scanner(System.in); 
        System.out.println("Would you like to convert from Fahrenheit to Celcius or Celcius to Fahrenheit?");
        System.out.println("(1) Fahrenheit to Celcius");
        System.out.println("(2) Celcius to Fahrenheit");
        int choice = in.nextInt(); //Get user input on whether to do F to C or C to F
        int celcius = 1;
        int fahrenheit = 2;
        if (choice != 1 && choice != 2)

        {
            System.out.println("That was not one of the choices."); //handle if they don't enter a valid choice.
            //System.out.println("Your must wear a coat" );
            System.out.println("Your need to wear t-shirt");
            }
            else if (choice == 1) //Fahrenheit to Celcius
            {
            System.out.println("Please enter the temperature in Fahrenheit to be converted to Celcius:");
            double toConvertFahrenheit = in.nextDouble(); //Get user input for value to convert... Used double to support decimals
            toConvertFahrenheit = (((toConvertFahrenheit-32)*5)/9); //subtract 32, multiply by 5, then divide by 9
            System.out.println("Your converted value is: " + toConvertFahrenheit + " degrees Celcius.");
            System.out.println("You Need to Wear a t-shirt");
            }
            else if (choice == 2)
            {
            System.out.println("Please enter the temperature in Fahrenheit to be converted to Celcius:");
            double toConvertCelcius = in.nextDouble();
            toConvertCelcius = (((toConvertCelcius*9)/5)+32); //multiply by 9, divide by 5, add 32.
            System.out.println("Your converted value is: " + toConvertCelcius + " degrees Fahrenheit.");

            // What to wear statement
            if (fahrenheit <= 32) {
                System.out.println("Your must wear a coat");

            }
            else if (celcius >= fahrenheit  ) {
                System.out.println("Your need to wear t-shirt");
            }

            } // comment 1


    } // comment 2

} // comment 3

How about using if-else in a different way...

if (choice==1 || choice==2) {  // only these 2 choices are valid
  // then check each choice
  if (choice==1) {
    ...
  }
  else {  // this condition is always choice==2
    ...
  }
}
else {  // anything else is invalid
}

If you do so, it would be easier to deal with comment because it will be inside its own choice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.