-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem08.java
More file actions
54 lines (44 loc) · 1.41 KB
/
Copy pathproblem08.java
File metadata and controls
54 lines (44 loc) · 1.41 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class problem08 {
// Read file
public static String readFile(String filename) {
String content = null;
File file = new File(filename);
try {
FileReader reader = new FileReader(file);
char[] chars = new char[(int) file.length()];
reader.read(chars);
content = new String(chars);
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
return content;
}
public static int findProd(String num) {
int product = 1;
for (int i = 0; i < num.length(); i++) {
product *= Character.getNumericValue(num.charAt(i));
}
return product;
}
public static void main (String args[]) {
int prod, largest = 0;
//Read numbers from 08.txt
String numbers = readFile("08.txt");
//Find largest product
for (int i = 0; i < numbers.length()-5; i++) {
String buffer = numbers.substring(i, i+5);
prod = findProd(numbers.substring(i, i+5));
System.out.println("Multiplying " + buffer);
System.out.println("Product: " + prod);
if (prod > largest) {
largest = prod;
}
}
System.out.println("\nLargest product: " + largest);
}
}