unreachable statement

Hi,

Can anyone find the problem? I've been scratching my head for hours wondering why the return statement would be unreachable... I figured someone with more experience would spot this in a couple of seconds.

publicstatic Teller newTeller(Teller t1, Teller t2){

int waarde, limiet, tellerNr;

char jn ='j';

boolean isCyclisch =false;

boolean correct =false;

waarde = IO.leesInt("Geef de waarde van de teller");

limiet = IO.leesInt("Geef de limiet van de teller");

jn = IO.leesChar("Moet de teller cyclisch zijn? (j/n): ");

if (jn =='j'){isCyclisch =true;}

while (!correct)

{

tellerNr = IO.leesInt("Geef het nummer van de teller");

switch (tellerNr)

{

case 1: t1 =new Teller(waarde, tellerNr, limiet, isCyclisch); correct =true;return t1;break;

case 2: t2 =new Teller(waarde, tellerNr, limiet, isCyclisch); correct =true;return t2;break;

default: System.out.println("Geen correct tellerNr!");returnnull;break;

}

}

}

Thanks!

[2329 byte] By [Nutkenza] at [2007-10-2 12:50:51]
# 1
You cannot reach the 'break' because you have already invoked 'return'. - Saish
Saisha at 2007-7-13 10:02:44 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

Hi,

That worked, I removed all the breaks, but now it keeps telling me the return statement is missing even though there's one in every option...

switch (tellerNr)

{

case 1: t1 = new Teller(waarde, tellerNr, limiet, isCyclisch); correct = true; return t1;

case 2: t2 = new Teller(waarde, tellerNr, limiet, isCyclisch); correct = true; return t2;

default: System.out.println("Geen correct tellerNr!"); return null;

}

Putting another return null statement at the bottom might solve it I suppose, but that seems so useless... is there a more refined way of getting rid of this error?

Nutkenza at 2007-7-13 10:02:44 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

javac checks whether return statement exists for all possible paths.'switch' statement is inside a 'while' statement. Yes, you have 'return' statement for all "cases" of switch. But, consider the case where the 'while' statement itself is not entered at all. For that path, you still need a 'return' statement.

sundararajan.aa at 2007-7-13 10:02:44 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

You have to have a return statement out side of the while loop. You can achieve this by assigning a variable inside the while loop so that when you reach the outside the variable is set by the path of loop and returned upon exiting.

public static Teller newTeller(Teller t1, Teller t2) {

int waarde, limiet, tellerNr;

char jn = 'j';

boolean isCyclisch = false;

boolean correct = false;

waarde = IO.leesInt("Geef de waarde van de teller");

limiet = IO.leesInt("Geef de limiet van de teller");

jn = IO.leesChar("Moet de teller cyclisch zijn? (j/n): ");

if (jn == 'j') {isCyclisch = true;}

t1 = new Teller();

while (!correct)

{

tellerNr = IO.leesInt("Geef het nummer van de teller");

switch (tellerNr)

{

case 1: t1.setAttribute(); break;

case 2: t1.setAttribute(); break;

}

}

return t1;

}

I am a novice at Java programming but I hope that this information is helpfull.

bryanbennetta at 2007-7-13 10:02:44 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5
Oops i forgot:correct=true;
bryanbennetta at 2007-7-13 10:02:44 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

I would suggest avoiding the breaks entirely:

public static Teller newTeller(Teller t1, Teller t2) {

int waarde, limiet, tellerNr;

char jn = 'j';

boolean isCyclisch = false;

waarde = IO.leesInt("Geef de waarde van de teller");

limiet = IO.leesInt("Geef de limiet van de teller");

jn = IO.leesChar("Moet de teller cyclisch zijn? (j/n): ");

if (jn == 'j') {isCyclisch = true;}

while (true)

{

tellerNr = IO.leesInt("Geef het nummer van de teller");

switch (tellerNr)

{

case 1: return new Teller(waarde, tellerNr, limiet, isCyclisch);

case 2: return new Teller(waarde, tellerNr, limiet, isCyclisch);

default: System.out.println("Geen correct tellerNr!");

}

}

}

This will loop until a 1 or 2 is entered, at which point it returns a

new Teller. The return calls will break the loop.

es5f2000a at 2007-7-13 10:02:44 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...