-
Notifications
You must be signed in to change notification settings - Fork 0
/
Euler22(Java)
60 lines (45 loc) · 1.35 KB
/
Euler22(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
56
57
58
59
60
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
public class Euclid22 {
/* In this program I was provided a list of names. My goal was to assign each name
a numerical value based on the sum of its characters (A=1, B=2, etc.) and its place
in the line.
*/
//Import file and run program
public static void main(String[] args) throws FileNotFoundException {
int total = 0;
Scanner s = new Scanner(new File("/Users/robertlippens/Desktop/names.txt")).useDelimiter(",");
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
}
s.close();
String[] Names = orderNames(list);
total = nameValue(Names);
}
//order names in file by using Collections.sort method
public static String[] orderNames (ArrayList<String> list) {
String[] oNames = new String[list.size()];
Collections.sort(list);
oNames = list.toArray(oNames);
return oNames;
}
//gives the names a numerical value based on pre-specified formula
public static int nameValue (String[] names) {
int total = 0;
int eachName = 0;
for (int i = 0; i < names.length; i++) {
for (char d : names[i].toCharArray()) {
eachName+=(d-64);
}
eachName+=60;
eachName*=i+1;
total+=eachName;
eachName = 0;
}
System.out.print(total);
return total;
}
}