473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

thatos
105 100+
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(Song.java:13)
at Song.main(Song.java:46)
Expand|Select|Wrap|Line Numbers
  1. /* 
  2. Most of code was created by Scott Hazelhurst 
  3. This program shows a rare example of where one doesn't use the break statement at the end of each case clause. 
  4.  
  5. Copy this program below into a file (you should be able to select the program using your mouse and paste it into your editor. 
  6. See what effect there would be if you put a break after each case clause. 
  7. */
  8.  
  9. public class Song {
  10. /** Shows an example of why sometimes you might
  11.     not want to use breaks in a Case 
  12. */
  13.     private static String toText(int num){
  14.         String res;
  15.         switch (num){
  16.         case 1:return "first";
  17.         case 2:return "second";
  18.         case 3:return "third";
  19.         case 4:return "forth";
  20.         case 5:return "fifth";
  21.         case 6:return "sixth";
  22.         case 7:return "seventh";
  23.         case 8:return "eigth";
  24.         case 9:return "ninth";
  25.         case 10:return "tenth";
  26.         case 11:return "eleventh";
  27.         case 12:return "twelth";
  28.         }
  29.     }
  30.  
  31.  
  32.  
  33. */
  34.     private static void prnt(String s) {
  35.     // Defined because System.out.println takes so much space on a line
  36.         System.out.println(s);
  37.     };
  38.  
  39.  
  40.     public static void main (String [] arg){
  41.  
  42.     int d;
  43.     String m;
  44.  
  45.     for (d=1; d<=12; d++) {
  46.         m = toText(d);
  47.         prnt
  48.           ("On the day " + m + " of Christmas, my true love sent to me");
  49.         switch (d) {
  50.         case 12: prnt("Twelve drummers drumming");
  51.         case 11: prnt("Eleven pipers piping");
  52.         case 10: prnt("Ten lords a-leaping");
  53.         case 9:  prnt("Nine ladies dancing");
  54.         case 8:  prnt("Eight maids a-milking");
  55.         case 7:  prnt("Seven swans a-swimming");
  56.         case 6:  prnt("Six geese a-laying");
  57.         case 5:  prnt("Five golden rings");
  58.         case 4:  prnt("Four calling birds");
  59.         case 3:  prnt("Three French hends");
  60.         case 2:  prnt("Two turtle doves");
  61.                prnt("And, ...");
  62.         case 1:  prnt("A Partridge in a pear tree");
  63.         }
  64.         prnt("");
  65.     }
  66.     }
  67. }
  68.  
  69.  
  70. /* This is a comment, ignored by the Java compiler
  71.  
  72. */ 
  73.  
Apr 19 '08 #1
3 1684
sukatoa
539 512MB
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(Song.java:13)
at Song.main(Song.java:46)
Expand|Select|Wrap|Line Numbers
  1. /* 
  2. Most of code was created by Scott Hazelhurst 
  3. This program shows a rare example of where one doesn't use the break statement at the end of each case clause. 
  4.  
  5. Copy this program below into a file (you should be able to select the program using your mouse and paste it into your editor. 
  6. See what effect there would be if you put a break after each case clause. 
  7. */
  8.  
  9. public class Song {
  10. /** Shows an example of why sometimes you might
  11.     not want to use breaks in a Case 
  12. */
  13.     private static String toText(int num){
  14.         String res;
  15.         switch (num){
  16.         case 1:return "first";
  17.         case 2:return "second";
  18.         case 3:return "third";
  19.         case 4:return "forth";
  20.         case 5:return "fifth";
  21.         case 6:return "sixth";
  22.         case 7:return "seventh";
  23.         case 8:return "eigth";
  24.         case 9:return "ninth";
  25.         case 10:return "tenth";
  26.         case 11:return "eleventh";
  27.         case 12:return "twelth";
  28.         }
  29.     }
  30.  
  31.  
  32.  
  33. */
  34.     private static void prnt(String s) {
  35.     // Defined because System.out.println takes so much space on a line
  36.         System.out.println(s);
  37.     };
  38.  
  39.  
  40.     public static void main (String [] arg){
  41.  
  42.     int d;
  43.     String m;
  44.  
  45.     for (d=1; d<=12; d++) {
  46.         m = toText(d);
  47.         prnt
  48.           ("On the day " + m + " of Christmas, my true love sent to me");
  49.         switch (d) {
  50.         case 12: prnt("Twelve drummers drumming");
  51.         case 11: prnt("Eleven pipers piping");
  52.         case 10: prnt("Ten lords a-leaping");
  53.         case 9:  prnt("Nine ladies dancing");
  54.         case 8:  prnt("Eight maids a-milking");
  55.         case 7:  prnt("Seven swans a-swimming");
  56.         case 6:  prnt("Six geese a-laying");
  57.         case 5:  prnt("Five golden rings");
  58.         case 4:  prnt("Four calling birds");
  59.         case 3:  prnt("Three French hends");
  60.         case 2:  prnt("Two turtle doves");
  61.                prnt("And, ...");
  62.         case 1:  prnt("A Partridge in a pear tree");
  63.         }
  64.         prnt("");
  65.     }
  66.     }
  67. }
  68.  
  69.  
  70. /* This is a comment, ignored by the Java compiler
  71.  
  72. */ 
  73.  
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
Apr 19 '08 #2
Ganon11
3,652 Expert 2GB
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."
Apr 19 '08 #3
thatos
105 100+
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
Apr 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Steven Green | last post by:
I have a java app at work I used when I had Windows 98 and never had a problem. I did a clean install of Windows XP and of course Java was not included. I went to Sun, download Java 2 Runtime...
3
by: Alex Bell | last post by:
I am still trying to design a two column layout but neither http://www.tassie.net.au/~abell1/meyer332.htm nor http://www.tassie.net.au/~abell1/testmenu1.htm work as they should Could some kind...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
5
by: Franco, Gustavo | last post by:
Hi, I have a question, and please I need a answer. How can I finalize a thread running with Application.Run (I need the message loop!!!) without call Thread.Abort?. I want to call...
2
by: Yoshitha | last post by:
Hi I want to create license protection for a web based application. when any user logging into the application it has to check the key enterd by the user against the database where the key along...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
9
by: Coleen | last post by:
Hi All :-) I am desperately looking for some help/information on how to direct page flow. Forget what I have done - here's what I need to do: I have a large ASPX.Net - VB.Net web application...
9
by: FERHAT AÇICI | last post by:
hi all! who know arrays on visual basic please tell me.... thanks..
2
by: cephal0n | last post by:
I have this peroblem thats really bugging me for days, please have a patience to read it and help me find the probplem because I knew I missed it and just cant tell where. I have a table named...
2
by: violeta123 | last post by:
I am stuck! Please help It might be difficult to explain the problem via email, but I will try. I have a Win 2003 Enterprise server running with the only purpose of a membership web site...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.