Data file:
J A V A
1 H 11 H 21 H
1 V 2 V 3 V
2 H 12 H 22 H
3 V 4 V 5 V
7 H 6 V 17 H
Code (part of a larger program):
File dataFile = new File("data.txt");
Scanner in;
in = new Scanner (dataFile);
String letter1 = in.next(); //Reads first letter of the word and stores it as a string
String letter2 = in.next(); //Reads second letter of the word and stores it as a string
String letter3 = in.next(); //Reads third letter of the word and stores it as a string
String letter4 = in.next(); //Reads fourth letter of the word and stores it as a string
// Not all of these ints are used in the code shown here
int firstLetterValue1;
int secondLetterValue1;
int thirdLetterValue1;
int fourthLetterValue1;
int firstLetterValue2;
int secondLetterValue2;
int thirdLetterValue2;
int fourthLetterValue2;
int firstLetterValue3;
int secondLetterValue3;
int thirdLetterValue3;
int fourthLetterValue3;
int multiplier1 = 1;
int multiplier2 = 1;
int multiplier3 = 1;
int totalWordValue1 = 0;
int totalWordValue2 = 0;
int totalWordValue3 = 0;
// These test to see whether the first, second, third, or fourth letter of the word
// Are equal to a specified letter and if they are,
// then a value is stored into a different integer
if (letter1.equalsIgnoreCase("A") || letter1.equalsIgnoreCase("E"))
{
firstLetterValue1 = 1;
firstLetterValue2 = 1;
firstLetterValue3 = 1;
}
else if (letter1.equalsIgnoreCase("D") || letter1.equalsIgnoreCase("R"))
{
firstLetterValue1 = 2;
firstLetterValue2 = 2;
firstLetterValue3 = 2;
}
else if (letter1.equalsIgnoreCase("B") || letter1.equalsIgnoreCase("M"))
{
firstLetterValue1 = 3;
firstLetterValue2 = 3;
firstLetterValue3 = 3;
}
else if (letter1.equalsIgnoreCase("V") || letter1.equalsIgnoreCase("Y"))
{
firstLetterValue1 = 4;
firstLetterValue2 = 4;
firstLetterValue3 = 4;
}
// For example, in the word "J A V A", the first letter, which is "J", will store
// the value "8" into the integers inside the brackets (firstLetterValue1, firstLetterValue2, etc.)
else if (letter1.equalsIgnoreCase("J") || letter1.equalsIgnoreCase("X"))
{
firstLetterValue1 = 8;
firstLetterValue2 = 8;
firstLetterValue3 = 8;
}
for (int num = 0; num <= 4; num++)
{
int location1 = in.nextInt();
String direction1 = in.next();
// This is where I am getting my problem: "The local variable firstLetterValue1 has not been initialized"
if ((location1 == 1) && (direction1.equalsIgnoreCase("H")))
{
firstLetterValue1 *= 1;
secondLetterValue1 *= 1;
thirdLetterValue1 *= 2;
fourthLetterValue1 *= 1;
multiplier1 = 1;
totalWordValue1 = (firstLetterValue1 + secondLetterValue1 + thirdLetterValue1 + fourthLetterValue1) * multiplier1;
}
}
I tried to outline my problem with the comments. So, I am getting an error at the part with if ((location1 == 1) && (direction1.equalsIgnoreCase("H")))
. I know that the program wants me to declare the integer by int firstLetterValue1 = 0;
, but the problem I am getting is that the whole part before it with the if
statements will just be skipped. I want the firstLetterValue1
, firstLetterValue2
, etc. integers to keep their value.
If this is still unclear let me know