Can you please tell me where the problem is,Please?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

    Can you please tell me where the problem is,Please?

    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)
    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
    
    */
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by thatos
    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)
    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
    
    */
    Maybe JVM tries to ask a question on your code: "What if num > 12 or num < 1? what should i return?".....

    Try to put a default on your switch.....

    regards,
    sukatoa

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Try adding a default to the switch, which will return some nonsense String (since you know it will never be reached), like "This is a useless return value."

      Comment

      • thatos
        New Member
        • Aug 2007
        • 105

        #4
        Originally posted by sukatoa
        Maybe JVM tries to ask a question on your code: "What if num > 12 or num < 1? what should i return?".....

        Try to put a default on your switch.....

        regards,
        sukatoa
        Thanks very much it really worked whn I placed the default

        Comment

        Working...