-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathNames_scores.java
37 lines (31 loc) · 1.03 KB
/
Names_scores.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
public class Names_scores {
public static void main(String[] args) {
//Initialize array containing names from text file
String fileName="names.txt";
String[] names = null;
try{
FileReader inputFile = new FileReader(fileName);
BufferedReader bufferReader = new BufferedReader(inputFile);
String line=bufferReader.readLine();
names=line.split(",");
bufferReader.close();
}
catch(Exception e){
}
//Sort array of names alphabetically
Arrays.sort(names);
//Iterate through array and calculate score, adding to total sum
int total_score=0;
for(int i=0;i<names.length;i++){
int score=0;
for(char letter:names[i].toCharArray()){
score+="ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(letter)+1;
}
total_score+=(score*(i+1));
}
System.out.println(total_score);
}
}