1

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

Evan
  • 870
  • 1
  • 12
  • 24
  • 2
    You won't be able to read `firstLetterValue1` unless it's assigned a value when the `if` condition is false. – Mike Samuel Jan 14 '14 at 00:14
  • where is your `for` loop? – Baby Jan 14 '14 at 00:16
  • Probably if condition never be true. check the conditions – Polymorphism Jan 14 '14 at 00:16
  • Possible duplicate of http://stackoverflow.com/questions/11165485/java-why-am-i-required-to-initialize-a-primitive-local-variable – Mike Samuel Jan 14 '14 at 00:17
  • 1
    In Java, in order to reference a variable of any type it must have been initialized. While object instance variables are initialized to zero/null on object creation, local variables are not. If there is some possible execution path from the variable declaration to the point of reference where the variable is not assigned a value then the reference will be disallowed. (This is critical to Java verification, and is not simply an arbitrary restriction.) – Hot Licks Jan 14 '14 at 00:21
  • 1. Why are you doing `*= 1` ? That does nothing. 2. You have to initialize your variables somewhere: either in the declaration at the top, or in an `else` block at the end of your if structure because it *needs* to have a value to be used in `*=` – Michael Yaworski Jan 14 '14 at 02:07
  • @mikeyaworski I know that `*= 1` does nothing. I have it because I have a lot of the `if-else` statements and it is easier to go through all of them and change what needs to be changed. Also, with making an `else` block - the problem with that is because the data in the text file can change (my teacher will edit the text file to see if my program will work for any given input) – Evan Jan 14 '14 at 02:16
  • So why can't you initialize the variable? – Michael Yaworski Jan 14 '14 at 02:20
  • Like doing `int = firstLetterValue1 = 0;`? It's because then the `if-else` statements outside the `for` loop are skipped - aren't they? – Evan Jan 14 '14 at 02:24
  • Ok I feel dumb now... I did what everyone said and turns out it worked... (originally I tried it but it didn't work, I don't know why :P) – Evan Jan 14 '14 at 02:29
  • What you just said before made no sense anyways. Alright, well then accept an answer. – Michael Yaworski Jan 14 '14 at 02:31

2 Answers2

4

You just have to assign default value to local variables that would make it compilable

int i = -1;

if( /* some condition */) {
  i = 10;
}

System.out.println(i);
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    this is not true. You do not need to initialize the primitive type. – 75inchpianist Jan 14 '14 at 00:17
  • Yeah, I don't think so either. – Robert Harvey Jan 14 '14 at 00:17
  • 6
    Why the downvotes? Local variables must be initialized before first use in Java. – Mike Samuel Jan 14 '14 at 00:18
  • 1
    Yes, you definitely do need to initialize a primitive that's locally declared. – Hot Licks Jan 14 '14 at 00:18
  • That's fine, but it doesn't appear to be the OP's problem. Removed my downvote anyway. – Robert Harvey Jan 14 '14 at 00:19
  • His code does not show use of the variables outside of the if, hence why I said they don't need to be initialized. in your example, however they do. – 75inchpianist Jan 14 '14 at 00:20
  • I had a sweet explanatory answer ready to go and then question closed =( – Tdorno Jan 14 '14 at 00:20
  • @75inchpianist - He said "I want to use the variable firstLetterValue1 outside of the brackets", which implies after the `if` statement. – Hot Licks Jan 14 '14 at 00:23
  • 1
    @HotLicks You don't, in general, need to initialize local variables. All that is required is that the variable is definitely assigned at each use. If I expect all paths to assign to a variable, I do not initialize it so that I'll get an error if I'm wrong, rather than silent use of an arbitrary initialization value. – Patricia Shanahan Jan 14 '14 at 02:03
  • @PatriciaShanahan - Well, that's playing with the terminology. The variable must be assigned along all paths. And any assignment to a previously unassigned variable is "initializing" it, by the meaning of the word. – Hot Licks Jan 14 '14 at 02:08
2

If none of the conditions in the if / else if is true, the variables won't be initialised. Two options:

  • initialise them with a default value when you declare them
  • add an else statement to ensure that the variables will always be initialised
assylias
  • 321,522
  • 82
  • 660
  • 783