How to create a java app that converts fahrenheit to celsius and allows you to list the cities you want converted in an input text file

   How to create a java app that converts fahrenheit to celsius and allows you to list the cities you want converted in an input text file







To create a java app that converts fahrenheit to celsius and allows you to read the input cities (and their default celsius) from an input text file, there are a number of small steps to take.

Once you master this app, you could easily reverse the order and convert celsius to fahrenheight, or you could advance to more complex capabilities.

This is another baby step that is worthy of practicing, by following, and then by aiming to complete more and more without referencing, and then completely by memory, and to practice often.

So let's do it, and discuss it, just a bit ...


Here's the code:

  1 import java.io.BufferedReader;

  2 import java.io.FileReader;

  3 import java.io.FileWriter;

  4 import java.io.IOException;

  5 import java.io.PrintWriter;

  6 

  7 public class FahrenheitToCelsius {

  8     public static void main(String[] args) {

  9         System.out.println("Welcome to the Fahrenheit to Celsius app!  Place the city & fahrenheight combinations (one line each) in input_fahrenheit    .txt within same directory and then watch for a created file named output_celsius.txt to be created with those same cities and their celsius temperat    ure after you run this app!");

 10 

 11         try {

 12             BufferedReader reader = new BufferedReader(new FileReader("input_fahrenheit.txt"));

 13             PrintWriter writer = new PrintWriter(new FileWriter("output_celsius.txt"));

 14 

 15             String line;

 16             while ((line = reader.readLine()) != null) {

 17                 String[] parts = line.split("\\s+");

 18                 String city = parts[0];

 19                 double fahrenheit = Double.parseDouble(parts[1]);

 20 

 21                 double celsius = (fahrenheit - 32) * 5 / 9;

 22                 writer.println(city + " " + String.format("%.2f", celsius));

 23             }

 24 

 25             reader.close();

 26             writer.close();

 27         } catch (IOException e) {

 28             e.printStackTrace();

 29         }

 30     }

 31 }

Here's what we used when building this code:

  • java 8
  • vim editor (note: we have much love for intellij and other editors too!)
  • code was placed in a file named FahrenheitToCelsius.java

You need to place the input cities and fahrenheits (separated by a space) in the input file.

This does restrict your ability to leverage cities that have spaces if you're not careful, which is one of the reasons that encapsulated your values in quotes, or using a csv file, are popular choices.

The contents of the input_fahrenheit.txt file:

st.louis 88

dallas 95

lansing 78

detroit 82

Here's what we did to compile and run the code:

  • javac FahrenheitToCelsius.java
  • java FahrenheitToCelsius

Here's what you'll see when you run it:

Welcome to the Fahrenheit to Celsius app!  Place the city & fahrenheight combinations (one line each) in input_fahrenheit.txt within same directory and then watch for a created file named output_celsius.txt to be created with those same cities and their celsius temperature after you run this app!

You'll notice the file output_celsius.txt created with a celsius value for each of the cities you had in your input file.

Here's a bit of description about the code and concepts used:

  • the public class name of FahrenheitToCelsius is the same as the filename, and that's important in order for the file class to compile
  • The syntax System.out.println is used for printing lines of content, and the content that you want to be printed belongs between the quotes for the text portion of values, and that's sufficient for this example.
  • The typical public static void main function is added and is the entry point to the application
  • We need to import additional capabilities that we can leverage instead of recreate.  In this example we import the following classes so that we can use them in our solution: BufferedReader, FileReader, FileWriter, IOException, and PrintWriter.
  • Searching for and discovering the correct capabilities that already exist is always a huge deal to help you maximize the quality of your product and to minimize the necessary code in your solution.
  • The classes above that are imported help us to open a file, parse a file, and write to a file.
  • Notice that we also need exception handling when dealing with files, hence the use of try and catch blocks for our code.
  • The decision to specifically catch an IOException is a very specific type of exception to try to catch and positions us to handle other kinds of exceptions differently with the addition of a bit more code.
  • The math for the fahrenheit to celsius conversion is actually all on one code (line 21).  The math itself is pretty simple, but the added complexity to allow flexibility on the input cities you want converted and the file handling requires a bit more code, on top of the required foundational boilerplate code.
  • The mechanism for parsing by spaces occurs on line 17.  You could easily change this to parse by comma separation or another means instead.

Another baby step completed, nice job!

Want to know how to do something in java or have any questions?  Let me know by commenting, and we'll consider getting there as we continue to take baby steps!

Copyright © 2024 correlateencourage - All Rights Reserved.

..............................

Practicing baby steps is one of the things we believe correlates with success, and keeping track of your practice experience is another.  It's one of the many things you can track with our correlateencourage app, available here at the apple store: https://apps.apple.com/us/app/correlateencourage/id6504427387  


Comments

Post a Comment

Popular posts from this blog

How to create a java app that creates a very simple plot from data in a csv file

Learn Java: The ABC's of Java