Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MARY PATRICIA LINDA BARBARA ELIZABETH JENNIFER MARIA SUSAN MARGARET DOROTHY LISA

ID: 3828063 • Letter: M

Question

MARY
PATRICIA
LINDA
BARBARA
ELIZABETH
JENNIFER
MARIA
SUSAN
MARGARET
DOROTHY
LISA
NANCY
KAREN
BETTY
HELEN
SANDRA
DONNA
CAROL
RUTH
SHARON
MICHELLE
LAURA
SARAH
KIMBERLY
DEBORAH
JESSICA
SHIRLEY
CYNTHIA
ANGELA
MELISSA
BRENDA
AMY
ANNA
REBECCA
VIRGINIA
KATHLEEN
PAMELA
MARTHA
DEBRA
AMANDA
STEPHANIE
CAROLYN
CHRISTINE
MARIE
JANET
CATHERINE
FRANCES
ANN
JOYCE
DIANE
ALICE
JULIE
HEATHER
TERESA
DORIS
GLORIA
EVELYN
JEAN
CHERYL
MILDRED
KATHERINE
JOAN
ASHLEY
JUDITH
ROSE
JANICE
KELLY
NICOLE
JUDY
CHRISTINA
KATHY
THERESA
BEVERLY
DENISE
TAMMY
IRENE
JANE
LORI
RACHEL
MARILYN
ANDREA
KATHRYN
LOUISE
SARA
ANNE
JACQUELINE
WANDA
BONNIE
JULIAYou will be using the provided names.txt file, text 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?

part of the txt file is copy pasted at the begining.

Explanation / Answer

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


public class NameScore {
  
  
  
public static void main(String[] args) throws FileNotFoundException
{
List<String> names = new ArrayList<>();
FileReader fr = new FileReader("names.txt");
Scanner sc = new Scanner(fr);
  
while(sc.hasNextLine())
{
names.add(sc.nextLine().trim());
}
sc.close();
  
Collections.sort(names);
int scoreSum = 0;
for(int i = 0; i < names.size(); i++)
{
int nameScore = 0;
String name = names.get(i);
for(int j = 0; j < name.length(); j++)
{
nameScore += (name.charAt(j) - 'A');
}
nameScore = nameScore*(i+1);
scoreSum += nameScore;
}
System.out.println("Score sum is : " + scoreSum);
}

}