patternjavaModerate
Project Euler 22: Assigns scores for names that are imported from text file
Viewed 0 times
fileimportedaretextnamesprojectthatforeulerscores
Problem
Project Euler 22
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 names = new ArrayList();
static ArrayList scores = new ArrayList();
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)
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 names = new ArrayList();
static ArrayList scores = new ArrayList();
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)
Solution
Reading the file: you declare the BufferedReader as an instance variable while it is only used locally. Don't do that: variables should always have the smallest scope possible. In addition, resources should normally be closed in the same scope that opens them (which brings me to the point that you never close the buffered reader.)
Furthermore, you put the opening and reading in two different try-catch constructs. That makes no sense. If the first fails, you cannot expect the second to work.
Thus, using "classic" code, you should put it together as a try-with-resources block, which automatically closes the opened resource, something like this:
But as this is 2017 and the library is quite mature, you should really simply use java.nio.Files:
is all you need.
There are quite a few more problems in your code (especially the mixup of static and instance variables), but alas my time is limited right now.
Furthermore, you put the opening and reading in two different try-catch constructs. That makes no sense. If the first fails, you cannot expect the second to work.
Thus, using "classic" code, you should put it together as a try-with-resources block, which automatically closes the opened resource, something like this:
try (BufferedReader reader = new BufferedReader(new FileReader(location))) {
String name;
while((name = reader.readLine()) != null) { // classic construct ;-)
names.add(name);
}
}
catch(IOException e) {
// Do something useful. Especially, do NOT continue program
// execution, as you don't have any data anyway!
}But as this is 2017 and the library is quite mature, you should really simply use java.nio.Files:
Files.readAllLines(Paths.get(location))is all you need.
There are quite a few more problems in your code (especially the mixup of static and instance variables), but alas my time is limited right now.
Code Snippets
try (BufferedReader reader = new BufferedReader(new FileReader(location))) {
String name;
while((name = reader.readLine()) != null) { // classic construct ;-)
names.add(name);
}
}
catch(IOException e) {
// Do something useful. Especially, do NOT continue program
// execution, as you don't have any data anyway!
}Files.readAllLines(Paths.get(location))Context
StackExchange Code Review Q#162890, answer score: 10
Revisions (0)
No revisions yet.