Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I would like my program to calculate fahrenheit into celcius and celcius into fahrenheit. The output needs to be like

Give a temperature in Celsius or Fahrenheit or type q to quit
>20.0 C
>20.0 degrees celcius = 68.0 degrees Fahrenheit
Give a temperature in Celsius or Fahrenheit or type q to quit
>68 F
>68.0 degrees Fahrenheit = 20 degrees Celcius
Give a temperature in Celsius or Fahrenheit or type q to quit
>q
> (quits the program)

Java
import java.util.Scanner;
public class RepeatedTempConversion {
    void converter() { 
        // You have to create different types of scanners 
    System.out.println("Give a temperature in Celsius or Fahrenheit or type q to quit");
    Scanner scan = new Scanner(System.in);
    String choice = scan.nextLine();
    if( choice == "q") { 
        System.exit(0); 
    }
    if (choice == temp + " C"){ 

    }
    if(choice == temp + " F")
    }
public static void main(String[] args) {
    new RepeatedTempConversion().converter(); 
    }
}


What I have tried:

I tried to set different scanners and also set fahrenheit and celcius variables as int and double, I have done the same for scanners as well however it just did not work. I got stuck on what I should do and how I should use scanners. If an example code can be sent I would be very happy. Thank you
Posted
Updated 16-Sep-20 20:39pm

This is plain wrong:
Java
if( choice == "q") {
    System.exit(0);
}
if (choice == temp + " C"){ //temp is not defined at this point
}
if(choice == temp + " F") //temp is not defined at this point
}

You need to analyze user input, that is parsing, you should learn to use RegEx.
A regEx allow you to describe legal user inputs and get the bits
Try to match this "(\d+) +(F|C)"

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
v3
I would have used another approach, to gather user selections:
Java
import java.util.Scanner;
  
public class TempConverter
{
  public static double celsiusToFahrenheit( double celsius)
  {
    return (celsius * 9.0 / 5.0 + 32.0);
  }

  public static double fahrenheitToCelsius( double fahrenheit)
  {
    return ((fahrenheit-32) * 5.0 / 9.0);
  }


  public static void main( String[] args )
  {
    Scanner scan = new Scanner(System.in);
    while (true)
    {
      System.out.println("Choose:\n'F' to enter a temperature in Fahrenheit. \n'C' to enter a temperature in Celsius\nAny other character to quit");
      String choice = scan.next();
      if ( choice.equals("F") )
      {
        System.out.println("Enter the temperature in Fahrenheit degrees");
        double fahrenheit = scan.nextDouble();
        System.out.printf("%.2f Fahrenheit degreees are %.2f Celsius degrees\n", fahrenheit, fahrenheitToCelsius(fahrenheit));
      }
      else if ( choice.equals("C") )
      {
        System.out.println("Enter the temperature in Celsius degrees");
        double celsius = scan.nextDouble();
        System.out.printf("%.2f Celsius degreees are %.2f Fahrenheit degrees\n", celsius, celsiusToFahrenheit(celsius));
      }
      else
        break;
    }
  }
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900