Using atext file containing over five-thousand first names, and you will calculate a score for each name. Begin by reading the names from the file and sorting them in alphabetical order. Then, you will compute an alphabetical value for each name where A is 1, B is 2, C is 3 and so on. The alphabetical value of a name is just the sum of the values of the letters in the name. To compute the name-score for a name, you simply multiply this alphabetical value by its position in the sorted list.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list.
So, the name-score for COLIN would be 938 * 53 = 49714. What is the sum total of all the name-scores in the file?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class ScoringNames {
BufferedReader x = null;
String location = "C:\\Users\\xxxxxxxi\\Dropbox\\"
+ "College\\COSC 1430\\Scoring Names\\names.txt";
ArrayList<String> names = new ArrayList<String>();
static ArrayList<Integer> scores = new ArrayList<Integer>();
static int ns[];
public void fillNS(){
for (int x = 0; x < ns.length; x++){
ns[x] = scores.get(x) * (x+1);
}
}
public void printNS(){
for (int x = 0; x < ns.length; x++)
System.out.print(ns[x] + ", ");
}
public void readFile(){ //Opens file, and prints every line.
try{
x = new BufferedReader(new FileReader(location));
} catch(FileNotFoundException e) {
e.printStackTrace();
}
try{
String name = x.readLine();
while(name != null){
//System.out.println(name);
names.add(name);
name = x.readLine();
}
} catch(IOException e) {
e.printStackTrace();
}
}
public void sortNames(){ //Sort names array in alphabetical order
Collections.sort(names);
}
public void printNames(){
for(String counter: names)
System.out.println(counter);
}
public int alphValue(char x){
return (x - 'A' + 1);
}
public void returnSize(){
System.out.println(names.size());
}
public int nameScore(String name){
int score = 0;
char[] tempName = name.toCharArray();
for (char i : tempName){
score += alphValue(i);
}
return score;
}
public void addScores(){
for(String x : names){
scores.add(nameScore(x));
}
}
public void printScores(){
for(int counter: scores)
System.out.println(counter);
}
public int totalNS(int[] x){
int sum = 0;
for(int i = 0; i < x.length; i++){
sum += x[i];
}
return sum;
}
public static void main(String[] args) {
ScoringNames abc = new ScoringNames();
abc.readFile();
abc.sortNames();
//abc.printNames();
abc.addScores();
//abc.printScores();
ns = new int[scores.size()];
abc.fillNS();
//abc.printNS();
//System.out.println(ns[937]); //"COLIN"
System.out.println("The sum total of all name scores is: " +
abc.totalNS(ns));
}
}