0

here is the question:

(Temperature Conversions) Implement the following integer methods: (a) Method celsius returns the Celsius equivalent of a Fahrenheit temperature, using the calculation celsius = 5.0 / 9.0 * ( fahrenheit - 32 ); (b) Method fahrenheit returns the Fahrenheit equivalent of a Celsius temperature, using the calculation fahrenheit = 9.0 / 5.0 * celsius + 32; (c) Use the methods from parts (a) and (b) to write an application that enables the user either to enter a Fahrenheit temperature and display the Celsius equivalent or to enter a Celsius temperature and display the Fahrenheit equivalent.

this is the method code: package Assignment.Q047;

public class TemperatureConversions 
{
    public double celsius( double fahrenheit )
    {
        double celsius = 5.0 / 9.0 * (fahrenheit - 32);
        return celsius;
    } 

    public double fahrenheit( double celsius )
    {
        double fahrenheit = 9.0 / 4.0 * (celsius + 32);
        return fahrenheit;
    } 
}

and here is the main function:

import java.util.Scanner;

public class TemperatureConversionsTest 
{
    public static void main(String args[])
    {
      Scanner input = new Scanner( System.in );
      TemperatureConversions test = new TemperatureConversions();
  
      int choice; 
      
      do
      {
         System.out.println( "******This is a temperature conversion method******" );
         System.out.println( "Enter '1': Coversion from Celsius to Fahrenheit" );
         System.out.println( "Enter '2': Coversion from Fahrenheit to Celsius" );
         System.out.println( "Enter '3': Exit Program" );
         System.out.print( "Choice: " );
         choice = input.nextInt();
         
         if ( choice != 3 )
         {
            System.out.print( "Enter temperature that need to be converted: " );
            double temperature = input.nextDouble();

            switch ( choice )
            {
                case 1:
                    double fahrenheit = test.fahrenheit(temperature);
                    System.out.printf( "%d Celsius is %d Fahrenheit\n" + temperature,     fahrenheit);
                    break;
                        
                case 2:
                    double celsius = test.celsius(temperature);
                    System.out.printf( "%d Fahrenheit is %d Celsius\n" + temperature, celsius);
                    break;
            } 
         }
      } while ( choice != 3 );
   }
}

I am using the Apache NEtbeans IDE to run the code. My code does not shows any error when it is typed . BUt once it is excuted and and entered the choice, it shows lot of errors.

below is the error:

******This is a temperature conversion method******
Enter '1': Coversion from Celsius to Fahrenheit
Enter '2': Coversion from Fahrenheit to Celsius
Enter '3': Exit Program
Choice: 2
Enter temperature that need to be converted: 98
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
    at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
    at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2938)
    at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2892)
    at java.base/java.util.Formatter.format(Formatter.java:2673)
    at java.base/java.io.PrintStream.format(PrintStream.java:1209)
    at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
    at Assignment.Q047.TemperatureConversionsTest.main(TemperatureConversionsTest.java:36)

Please help solve this problem as i am new to Java programming.

Stewart
  • 17,616
  • 8
  • 52
  • 80
  • 3
    `%d` is the format code for integers. Use `%f`. – 001 Jun 28 '21 at 13:34
  • 2
    You already got the hin here: `java.util.IllegalFormatConversionException: d != java.lang.Double` in your `printf()` call at `TemperatureConversionsTest.java` line 36 (you might need to learn how to read stack traces as this is essential). This means `"%d"` isn't meant for floating point vaues, use `"%f"` instead. Also, `printf( "%d Celsius is %d Fahrenheit\n" + temperature, fahrenheit)` is wrong - you're defining 2 placeholders but only pass 1 parameter as `temperature` is _directly_ added to the string. You need to replace `+` with a comma here. – Thomas Jun 28 '21 at 13:34
  • 2
    See [here](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html#syntax) for the list of valid format codes and what they mean. – Federico klez Culloca Jun 28 '21 at 13:34
  • The real answer here: you have to understand every character you put into your code. In other words: dont just take some example code you found somewhere. Take the time to read documentation. https://beginnersbook.com/2017/10/java-string-format-method/ – GhostCat Jun 28 '21 at 13:40
  • Thank you very much for your help #Johhny Mopp and #Thomas. my code work now. So sorry as i am beginner to java programming, so i am not so sure to understand the error in the IDE. Thank you so much. – Shangkari Sha Jun 29 '21 at 01:12

0 Answers0