-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileIOSolutions.java
55 lines (47 loc) · 1.9 KB
/
FileIOSolutions.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Name: Benjamin Bush
// Imports everything needed
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.IOException;
public class FileIOSolutions {
static Scanner scanner;
static PrintWriter pw;
public static void pastTense(String input, String output) {
// Instantiate file input/output and catches IOException to avoid errors
try {
scanner = new Scanner(new FileReader(input));
pw = new PrintWriter(output);
}
catch (IOException e) {}
// Run while there are more lines to parse
while (scanner.hasNextLine()) {
// Parses words and then changes them as necessary before outputing it
String[] words = scanner.nextLine().split(" ");
for (String word : words) {
String result = word.toUpperCase().equals("IS") ? "was" : word;
System.out.println(result);
pw.write(result);
}
}
// Closes input and output
scanner.close(); pw.close();
}
public static double totalTubeVolume(String input) {
// Instantiate file input and catches IOException to avoid errors
try {scanner = new Scanner(new FileReader(input));}
catch (IOException e) {}
double volume = 0;
// Run while there are more lines to parse
while (scanner.hasNextLine()) {
// Parses input and calculates/sums volume as per the formula
StringTokenizer st = new StringTokenizer(scanner.nextLine());
st.nextToken(); st.nextToken();
volume += Math.pow(Double.parseDouble(st.nextToken()), 2) * Math.PI * Double.parseDouble(st.nextToken());
}
// Closes input and returns
scanner.close();
return volume;
}
}