Advertisement
advictoriam

Untitled

Mar 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. /**
  2.    Reads a file of month names and average temperatures in Celsius,
  3.    and writes a file containing month names and average temperatures
  4.    in Celsius.
  5. */
  6.  
  7. import java.io.FileReader;
  8. import java.io.FileNotFoundException;
  9. import java.util.Scanner;
  10. import java.io.PrintWriter;
  11.  
  12. public class CityTemps
  13. {
  14.    public static void main(String[] args)
  15.       throws FileNotFoundException
  16.    {
  17.       String inputFileName = "celsius.txt";
  18.       String outputFileName = "fahrenheit.txt";
  19.  
  20.       FileReader fr = new FileReader(inputFileName);
  21.       PrintWriter writer = new PrintWriter(outputFileName);
  22.      
  23.       try
  24.       {
  25.          String c = "";
  26.          while(true)
  27.          {
  28.             int ch = fr.read();
  29.          
  30.             if(ch == -1)
  31.                break;
  32.             else if((char)ch == '\n')
  33.             {
  34.                writer.write(String.format("%f\n", (float)((Float.parseFloat(c)*(9/5))+32)));
  35.                c = "";
  36.             }
  37.             else if(Character.isDigit((char)ch) || (char)ch == '.')
  38.                c += (char)ch;
  39.             writer.write((char)ch);
  40.          }
  41.       }
  42.       catch(Exception e){}
  43.      
  44.       try
  45.       {
  46.          fr.close();
  47.          writer.close();
  48.       }
  49.       catch(Exception e){}
  50.    }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement