1

I'm using a variable line, which after the method turns into lineOld. So for the first time running the method lineOld is undefined.

So while compiling it gives me the error variable lineOld might not have been initialized.

I've tried to give lineOld a value just once (See first for and while), but no go.

Any help :) ?

String inputLine;
String lineOld;
String line = "";
int test = 0;

for (int i = 0; i == 0; i++) {
    lineOld = "";
}

while (test == 0) {
    lineOld = "";
    test++;
}

while ((inputLine = in.readLine()) != null) {
    line = line + inputLine;
}

System.out.println(line);

if (line == lineOld) {
    System.out.println("No difference");
} else {
    System.out.println("VERSCHIL");
}

lineOld = line;
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • 5
    Why can't you just initialize them like `line`? Also: [compare strings with `.equals()`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Ps: that `lineOld` initializer is.. rather ugly. – Jeroen Vannevel Apr 11 '14 at 15:39
  • Because this method keeps repeating, so I dont want lineOld to be reset every time. – user3524385 Apr 11 '14 at 15:42
  • 3
    lineOld can't hold its value between calls unless it's a global variable. – pennstatephil Apr 11 '14 at 15:43
  • Even if that was a good approach, it would still not be a good idea because it will still reset your `lineOld` every time the method is called. I suggest that, once you get your code fixed and working, you visit [CodeReview.se](http://codereview.stackexchange.com/) so we can provide you with a more indepth explanation on how your program *should* work. – Jeroen Vannevel Apr 11 '14 at 15:44
  • possible duplicate of [Variable might not have been initialized error](http://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – Brian Roach Apr 11 '14 at 15:50

6 Answers6

3

Simply initialize lineOld to some dummy variable, either "" or null.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
2

The compiler is pointing it out because there is a possibility that the for/while blocks do not execute, which would mean that lineOld is not initialized. It's looking for you to initialize it outside of a block.

Ken
  • 369
  • 1
  • 3
  • 10
1

On initialisation set lineOld to null

String lineOld = null;
Harry
  • 1,362
  • 12
  • 19
1

The underlying problem here, is that (as far as the compiler can see) it is possible for for this code to follow a path that will result in lineOld not being initialized prior to its use.

To fix this, you will need to initialize it outside of a conditional construct.

String lineOld = "";

"Because this method keeps repeating, so I dont want lineOld to be reset every time."

In that case, pass lineOld to this method. Check if the passed-in lineOld has a value, and initialize the local lineOld to it. If the passed-in lineOld does not have a value, then initialize the local lineOld to either a null or empty string.

Aaron
  • 55,518
  • 11
  • 116
  • 132
0

The compiler giving correct error because at this line, if(line == lineOld){ there might be a possibility that above for/while blocks do not execute, which means that lineOld is not initialized.

You can remove this error by initializing it to some dummy variable "" or null.

hatellla
  • 4,796
  • 8
  • 49
  • 101
0

Why do you use while or for controle to initialize your variable ? you can use if :

 if(test == 0){
    lineOld = "";
    test++;
}

but the better way I think is to initialize lineOld at null or "" then use this test :

 if(line.equals(lineOld))
cOOb
  • 3
  • 1