-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
66 lines (57 loc) · 1.33 KB
/
Main.java
File metadata and controls
66 lines (57 loc) · 1.33 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
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
Main m = new Main();
FileReader x = m.openFile(args[0]);
BufferedReader y = new BufferedReader(x);
String line;
try {
while((line = y.readLine()) != null){
processLine(line);
}
} catch (IOException e) {
System.out.println("Error reading line");
e.printStackTrace();
}
m.closeFile(x);
}
private static void processLine(String line) {
String[] xn = line.split(",");
int x = Integer.parseInt(xn[0]);
int n = Integer.parseInt(xn[1]);
x = smallestMultiple(x,n);
System.out.println(x);
}
private static int smallestMultiple(int x,int n) {
//System.out.println("x:" + x + " n:" + n);
while(x<=n){
if(x==n){
return x;
} else x++;
}
n = n * 2;
return smallestMultiple(x,n);
}
private FileReader openFile(String file) {
FileReader x = null;
try {
x = new FileReader(new File(file));
} catch (FileNotFoundException e) {
System.out.println("Error reading file.");
e.printStackTrace();
}
return x;
}
private void closeFile(FileReader x) {
try {
x.close();
} catch (IOException e) {
System.out.println("Error closing file.");
e.printStackTrace();
}
}
}