0

Hi I am trying to make a small temperature converter but I cannot input what unit I'm starting from. What is wrong?

import java.util.Scanner;

public class temperatureConvertor {
    
    public static void main(String[] args) throws InterruptedException {
        Scanner sc = new Scanner(System.in);


        System.out.println("Hello and thank you for testing this temperature convertor");
        Thread.sleep(700);        
        System.out.println("You can convert from Fahrenheit to Celsius and vice-versa");
        Thread.sleep(650);
        System.out.println("Let's begin!");
        System.out.println();
        System.out.println();

        System.out.println("Enter a temperature: ");
        int temperatureNumber = sc.nextInt();
        int newCel = ((temperatureNumber-32) * 5/9);
        int newFah = ((temperatureNumber * 9/5) + 32);

        Thread.sleep(500);
        System.out.println("Is this in fahrenheit or celsius? You can type fahrenheit/f or celsius/c ");
        String unit = sc.nextLine();
        Thread.sleep(1000);

        if(unit.equals("fahrenheit") || unit.equals("f")){
            System.out.println("Starting Temperature: " + temperatureNumber + "°F");
            Thread.sleep(600);
            System.out.println("New Temperature: " + newCel);
        }else if(unit.equals("celsius") || unit.equals("c")){
            System.out.println("Starting Temperature: " + temperatureNumber + "°C");
            Thread.sleep(600);
            System.out.println("New Temperature: " + newFah);
        }

        sc.close();
    }

}

Current output looks like that:

CurrentOutput

I tried to putting the conversion variables before the starting temperature and I tried putting the conversions in each conditonal.

Rafael
  • 1,281
  • 2
  • 10
  • 35
b0ngo
  • 9
  • 1
  • 2
    `sc.nextInt();` is leaving a dangling new line character in the input buffer, which is been read by the next `sc.nextLine();` - either use `sc.nextLine();` exclusively and parse the resulting value manually or add an additional `sc.nextLine();` after your `nextInt` calls – MadProgrammer Apr 03 '23 at 22:03
  • On a different note, why are you using `Thread.sleep()`? And your conversions won't always be accurate due to integer arithmetic. Best to use doubles. – WJS Apr 03 '23 at 22:10

0 Answers0