Problem 22. Names scores

Description

import java.io.*;
import java.lang.*;
import java.util.Arrays;

public class euler22 {
    public static void main(String[]args) {
        String line = "";
       	try {
            BufferedReader br = new BufferedReader(new FileReader("names.txt"));
            StringBuilder sb = new StringBuilder();
            line = br.readLine();
        }
      	catch (IOException e) {
            System.out.println(e.getMessage());
       	} 
       	String[] array = line.split(",");
       	String[] names = new String[array.length];
       	for (int i = 0; i < array.length; i++)
            names[i] = array[i].substring(1, array[i].length()-1);

       	Arrays.sort(names);
        int sum = 0;
       	for (int i = 0; i < names.length; i++)
            sum += score(names[i])*(i+1);
       	System.out.println(sum);
    }
    public static int score(String s) {
        int sum = 0;
        for (int i = 0; i < s.length(); i++)
            sum += s.charAt(i) - 64;
       	return sum;
    }
} 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>