mardi 24 février 2015

Scrabble Sum Numbers in File



I am supposed to take in string of letters and return the score as an integer by summing the score of each individual letter. The issue that I am having is that I was supposed to put that information from the file line by line below into the array (which I did), but I don't know how I am supposed to take that array and correspond the letter to the number. I feel like I need to create an associative array, but then there would be no point to creating the first array. I hope this makes sense, if not please let me know.



A 1
B 3
C 3
D 2
E 1
F 4
G 2
H 4
I 1
J 8
K 5
L 1
M 3
N 1
O 1
P 2
Q 10
R 1
S 1
T 1
U 1
V 4
W 4
X 8
Y 4
Z 10


Here is the actual code that I have written thus far, and the ScoreSum method is where I am having the issues.



import java.io.*;
import java.util.Scanner;

public class ScrabbleSolver {
String inputFile = "letterVals.txt";
String[]lines = new String[26];

public static void main(String[] args)
{
ScrabbleSolver scrabble = new ScrabbleSolver();
scrabble.UserInput();
scrabble.ScrabbleScore();
}

public ScrabbleSolver()
{
try
{
//Create stream for reading file
FileReader fin = new FileReader(inputFile);
//Create input stream for program
BufferedReader read = new BufferedReader( fin);

String nextLine = read.readLine();
int i = 0;
while ( nextLine != null)
{
lines[i] = nextLine;
nextLine = read.readLine();
i++;
}
read.close();
}
catch (IOException e)
{
System.err.println("Error while inputing file.");
System.err.println(e.getStackTrace());
}
}

public int ScoreSum( )
{
String eWord;
int sum = 0;
for (int i = 0; i < eWord.length(); i++)
{
//int w= Integer.parseInt(eWord);
sum += lines[eWord.charAt(i)];
System.out.println(lines[i]);
}
System.out.println("Score = " + sum);
return sum;
}
}



Aucun commentaire:

Enregistrer un commentaire