I run the following code on Eclipse and it gave me the following error:
This method must return a result of type String
at Song.toText(Son g.java:13)
at Song.main(Song. java:46)
This method must return a result of type String
at Song.toText(Son g.java:13)
at Song.main(Song. java:46)
Code:
/*
Most of code was created by Scott Hazelhurst
This program shows a rare example of where one doesn't use the break statement at the end of each case clause.
Copy this program below into a file (you should be able to select the program using your mouse and paste it into your editor.
See what effect there would be if you put a break after each case clause.
*/
public class Song {
/** Shows an example of why sometimes you might
not want to use breaks in a Case
*/
private static String toText(int num){
String res;
switch (num){
case 1:return "first";
case 2:return "second";
case 3:return "third";
case 4:return "forth";
case 5:return "fifth";
case 6:return "sixth";
case 7:return "seventh";
case 8:return "eigth";
case 9:return "ninth";
case 10:return "tenth";
case 11:return "eleventh";
case 12:return "twelth";
}
}
*/
private static void prnt(String s) {
// Defined because System.out.println takes so much space on a line
System.out.println(s);
};
public static void main (String [] arg){
int d;
String m;
for (d=1; d<=12; d++) {
m = toText(d);
prnt
("On the day " + m + " of Christmas, my true love sent to me");
switch (d) {
case 12: prnt("Twelve drummers drumming");
case 11: prnt("Eleven pipers piping");
case 10: prnt("Ten lords a-leaping");
case 9: prnt("Nine ladies dancing");
case 8: prnt("Eight maids a-milking");
case 7: prnt("Seven swans a-swimming");
case 6: prnt("Six geese a-laying");
case 5: prnt("Five golden rings");
case 4: prnt("Four calling birds");
case 3: prnt("Three French hends");
case 2: prnt("Two turtle doves");
prnt("And, ...");
case 1: prnt("A Partridge in a pear tree");
}
prnt("");
}
}
}
/* This is a comment, ignored by the Java compiler
*/
Comment