Okay here's what I have... but my test cases when running the program are not allowing me to input negative, decimal, or exponents as temperatures. I know it something small but it's alluding me, I figured if I had F and C as double it would work. Any ideas?

Another problem I seem to have (though very minor) is limiting the amount of numbers in a remainder to just the tenths place. Say you input 64°F and you convert to C, it comes out as 17.77777777777778°C. Like I said it's not a major problem, it just looks neat.

Thanks!

import java.util.Scanner;

public class Main {
      double fahrenheit;
      double celsius;
      int choice;
      Scanner input = new Scanner(System.in);

 public void mainMenu(){
        System.out.print("Welcome to the Fahrenheit to Celsius Converter!\n");
        System.out.println("Select from the following menu options below:\n");
        System.out.println("========================");
        System.out.println("| [1]  F to C          |");
        System.out.println("| [2]  C to F          |");
        System.out.println("| [3]  Exit            |");
        System.out.println("========================");
        System.out.print("Please select your option now:");
        choice =input.nextInt();

if(choice == 1){
    System.out.println("You have selected F to C.");
    System.out.print("Enter in the temperature: ");
    fahrenheit = input.nextInt();
    celsius = (5.0/9)*(fahrenheit - 32);
        System.out.println("The conversion is " + celsius + "\u00B0" + "C.");
}
else if(choice == 2){
    System.out.print("You have selected C to F.");
    System.out.print("Enter in the temperature: ");
    celsius = input.nextInt();
    fahrenheit = (9.0/5)*(celsius + 32);
        System.out.println("The conversion is " + fahrenheit + "\u00B0" + "F.");
}
      }

    public static void main(String[] args) {
    new Main().mainMenu();
    }
}

Recommended Answers

All 3 Replies

Okay now it wants print out negative numbers... but it won't input exponents and negative decimals.

Here is your solution for rounding off. If you cannot figure what that is saying, then do ask.
As for the values not being accepted, here are some mistakes in your code:

fahrenheit = input.nextInt();

You are telling the compiler to accept the input as an integer and that would explain why it wouldn't accept DECIMAL as they are NOT INTEGERS.

look into that.

It should accept negative values :/ It does for me. Perhaps you could show us what your test cases/input(files or otherwise) look like? Maybe then we can figure out what's wrong.

commented: Thanks for the insight! +1

Here is your solution for rounding off. If you cannot figure what that is saying, then do ask.
As for the values not being accepted, here are some mistakes in your code:

fahrenheit = input.nextInt();

You are telling the compiler to accept the input as an integer and that would explain why it wouldn't accept DECIMAL as they are NOT INTEGERS.

look into that.

It should accept negative values :/ It does for me. Perhaps you could show us what your test cases/input(files or otherwise) look like? Maybe then we can figure out what's wrong.

Oh okay I get it now about the input.nextInt().. it makes perfect sense. I knew it was something small.. thanks! *FacePalm*

Everything works as it should now!

Though the decimal rounding is going to take a bit of the Matrix "spoon bending" for me right now... I am going to have to research some more on what you provided for me.

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.