-
switch statement
i was hopeing to be able to use a switch statement like this, but found out you can't use strings. i can't use char, since 16 is there. how else can i set this up?
Code:
// Get the length of the note
public static String findLengthOfNote(String noteLength) {
char lastNumber = noteLength.charAt(noteLength.length()-1);
String number = Character.toString(lastNumber);
switch (number) {
case "2": noteLength = "Half - note"; break;
case "4": noteLength = "Fourth - note"; break;
case "8": noteLength = "One-eigth note"; break;
case "16": noteLength = "One-sixteenth note"; break;
default: noteLength = "Invalid length note."; break;
}
return noteLength;
}
-
Re: switch statement
Java SE 7 will support Strings in switch statements, but currently cannot.
You could cast the number String to an int:
Code:
int myInt = Integer.parseInt(number);
Then use myInt in your switch statement.
That or use a lot of if else statements instead of the switch statement.
-
Re: switch statement
Use a Map<String, String> to map from the number String to the text String you want to use.