java - Compiler thinks final instance variable not initialised -


my compiler warning me that instance variable, 2d int[][] array, might not have been initialised when go assign it.

i understand why compiler might think that, because initialised in double if statement. first if on boolean initialised true, , second if throws exception on else. confident of logic of program compiler not.

does have tips overcoming kind of problem? don't want otherwise initialise variable because meant final.

the variable of concern board variable. below part of constructor object contains variable.

    try {         filereader fr = new filereader(filename);         bufferedreader br = new bufferedreader(fr);         boolean first = true;         int linecount = 0;         string line;          while ((line = br.readline()) != null) {             string lineparts[] = line.split(" ");             if (first) {                 if (lineparts.length == 2) {                     this.xsize = integer.parseint(lineparts[0]);                     this.ysize = integer.parseint(lineparts[1]);                     board = new int[this.ysize][this.xsize];                     first = false;                 } else { throw new runtimeexception(); }             } else {                 linecount++;                 if (lineparts.length == this.xsize) {                     (int = 0; < this.xsize; i++) {                         board[linecount][i] = integer.parseint(lineparts[i]);                     }                 } else throw new runtimeexception();             }         }         br.close();         if (linecount != this.ysize) throw new runtimeexception();       }  

indeed, compiler can't unravel loop logic enough know final variable initialized before use.

you'll need move handling of first line out of loop — reasonable anyway, since content of loop different first line , subsequent lines:

try {     filereader fr = new filereader(filename);     bufferedreader br = new bufferedreader(fr);     int linecount = 0;     string line;      line = br.readline();     if (line != null) {         string lineparts[] = line.split(" ");         if (lineparts.length == 2) {             this.xsize = integer.parseint(lineparts[0]);             this.ysize = integer.parseint(lineparts[1]);             board = new int[this.ysize][this.xsize];         } else {             throw new runtimeexception();         }         while ((line = br.readline()) != null) {             string lineparts[] = line.split(" ");             linecount++;             if (lineparts.length == this.xsize) {                 (int = 0; < this.xsize; i++) {                     board[linecount][i] = integer.parseint(lineparts[i]);                 }             } else {                 throw new runtimeexception();             }         }     }     br.close();     if (linecount != this.ysize) throw new runtimeexception(); }  

note: code preserves previous code's behavior doesn't count first line. i'm guessing fact it's special includes not counting it. :-)


side note: i'd recommend using try-with-resources in code, not best practices, because you're not closing file when throw exceptions:

try (     filereader fr = new filereader(filename);     bufferedreader br = new bufferedreader(fr); ) {     int linecount = 0;     string line;      line = br.readline();     if (line != null) {         string lineparts[] = line.split(" ");         if (lineparts.length == 2) {             this.xsize = integer.parseint(lineparts[0]);             this.ysize = integer.parseint(lineparts[1]);             board = new int[this.ysize][this.xsize];         } else {             throw new runtimeexception();         }         while ((line = br.readline()) != null) {             string lineparts[] = line.split(" ");             linecount++;             if (lineparts.length == this.xsize) {                 (int = 0; < this.xsize; i++) {                     board[linecount][i] = integer.parseint(lineparts[i]);                 }             } else {                 throw new runtimeexception();             }         }     }     if (linecount != this.ysize) throw new runtimeexception(); }  

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -