-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
31 lines (28 loc) · 1.18 KB
/
main.java
File metadata and controls
31 lines (28 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.*;
import java.util.*;
public class Main
{
public static final int NUMITEMS = 20;
public static void main(String[] args) throws Exception
{
Scanner infile = new Scanner( new File("data.txt") );
double[] fahrenheit = new double[NUMITEMS];
double[] celsius = new double[NUMITEMS];
for(int x = 0; x < NUMITEMS; x++)
{
fahrenheit[x] = infile.nextDouble();
celsius[x] = ((int)(100 * ((5 * (fahrenheit[x] - 32)) / 9.0))) / 100;
}
try (FileWriter fileWriter = new FileWriter("Main.java");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
bufferedWriter.write("Fahrenheit\t | Celsius\n");
bufferedWriter.write("----------\t | --------------\n");
for(int i = 0; i < NUMITEMS; i++)
bufferedWriter.write(""+ fahrenheit[i] + "\t\t" + " | " + celsius[i] + "\n");
System.out.println("Successfully wrote to the file with BufferedWriter.");
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
e.printStackTrace();
}
}
}