-
Notifications
You must be signed in to change notification settings - Fork 0
/
DayOne_2.java
46 lines (44 loc) · 1.25 KB
/
DayOne_2.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
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
public class DayOne_2 {
public static void main(String[] args) {
int somma = 0;
int risultato = 0;
int uno = 0;
int due = 0;
int tre = 0;
try {
File myObj = new File("testDayOne.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
if(data.isEmpty() == true){
if(somma > uno){
tre = due;
due = uno;
uno = somma;
somma = 0;
}else if(somma > due){
tre = due;
due = somma;
somma = 0;
}else if(somma > tre){
tre = somma;
somma = 0;
}else{
somma = 0;
}
}else{
somma = somma + Integer.valueOf(data);
}
risultato = uno + due + tre;
}
System.out.print(risultato);
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}