Reputation: 542
Am I allowed to have a switch
statement which decides what to return? For example, I want to return something different based on what my random generator come up with. Eclipse is giving me an error wanting me to put the return
statement outside the switch
.
My code:
public String wordBank() { //Error here saying: "This method must return a type of string"
String[] wordsShapes = new String[10];
wordsShapes[1] = "square";
wordsShapes[2] = "circle";
wordsShapes[3] = "cone";
wordsShapes[4] = "prisim";
wordsShapes[5] = "cube";
wordsShapes[6] = "cylinder";
wordsShapes[7] = "triangle";
wordsShapes[8] = "star";
wordsShapes[9] = "moon";
wordsShapes[10] = "paralellogram";
Random rand = new Random();
int i = rand.nextInt(11);
if (i == 0) {
i = rand.nextInt(11);
}
switch (i) {
case 1:
return wordsShapes[1].toString();
case 2:
return wordsShapes[2].toString();
case 3:
return wordsShapes[3].toString();
case 4:
return wordsShapes[4].toString();
case 5:
return wordsShapes[5].toString();
case 6:
return wordsShapes[6].toString();
case 7:
return wordsShapes[7].toString();
case 8:
return wordsShapes[8].toString();
case 9:
return wordsShapes[9].toString();
case 10:
return wordsShapes[10].toString();
}
}
Upvotes: 11
Views: 17364
Reputation: 1738
You can simply do:
return wordsShapes[i].toString();
This way you can avoid the switch and all.
Upvotes: 12
Reputation: 1
The return statement will return from the entire function in which it is used.So I think it is good that no other useful lines of code must be there below the switch if you want to use the return statement in the switch.
Upvotes: 0
Reputation: 41087
You can put return
inside switch
but you don't need to use switch
in this case.
Upvotes: 6
Reputation: 88378
The problem is not that you have return statements inside the switch
statement, which are perfectly fine, but you have no return after the switch statement. If your switch statement completes without returning, what will happen now?
The rules of Java require that all paths through a value-returning function encounter a return
statement. In your case, even though you know the value of i
will always be of a value that will cause a return
from the switch, the Java compiler isn't smart enough to determine that.
(ASIDE: By the way, you didn't actually prevent the value 0 from being generated; maybe your if
should be a while
.)
ADDENDUM: In case you are interested, here's an implementation. See http://ideone.com/IpIwis for the live example.
import java.util.Random;
class Main {
private static final Random random = new Random();
private static final String[] SHAPES = {
"square", "circle", "cone", "prism", "cube", "cylinder", "triangle",
"star", "moon", "parallelogram"
};
public static String randomShapeWord() {
return SHAPES[random.nextInt(SHAPES.length)];
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
System.out.println(randomShapeWord());
}
}
}
Note the best practice of having the random number generator defined outside the function.
Upvotes: 4