Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Missing return statement

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Missing return statement

    Ok all, I have been racking my brain for the last 4 hours. I can not for the life of my figure out return statement I need for my code. (Which is located at the last method; public static String convertIntegerToWords) This is for school and only the second time I have ever had to do this. I hope that I can get some help. Here is my code: ( I apologize now for any formatting errors) Thanks to all in advance.

    import java.util.Scanner;
    public class marc1 {
     
    public static void main(String [] args) {
    //Main Method//
     
    //Create a Scanner//
        Scanner input = new Scanner(System.in);
     
        //Enter an Integer//   
         System.out.print(" What is your integer ? ");
       int Number= input.nextInt(); 
              while (Number >= 0) {
     
    	if (Number == 0) {
    	System.out.print( "Thank you for playing! " + "Good bye! ");	
        break;    }
     
    	else if (Number != 0)
    	validateLength(Number);
         break;  
       }   
    }   
       //Next Method//
      public static boolean validateLength(int userNum) {
    		String userNumber = "" + userNum;
     
    	if (userNumber.length() < 1) {
    	System.out.print("The number you entered is too long!");
    	  return false;
            }
     
    	else
    	  return true;
          } 
       //End of validate//
     
       //Final Method//
        public static String convertIntegerToWords(int Number) {
         if (Number == 1)
             System.out.println("Your integer " + Number + "is written out as one");
         else if (Number == 2) 
             System.out.println("Your integer " + Number + "is written out as two");
         else if (Number == 3) 
             System.out.println("Your integer " + Number + "is written out as three");
         else if (Number == 4) 
             System.out.println("Your integer " + Number + "is written out as four");
         else if (Number == 5) 
             System.out.println("Your integer " + Number + "is written out as five");
         else if (Number == 6) 
             System.out.println("Your integer " + Number + "is written out as six");
         else if (Number == 7) 
             System.out.println("Your integer " + Number + "is written out as seven");
         else if (Number == 8) 
             System.out.println("Your integer " + Number + "is written out as eight");
         else if (Number == 9) 
             System.out.println("Your integer " + Number + "is written out as nine");
     
        }
        }


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Missing return statement

    remember that all class names should be capitalized!
    change the class name to Marc1

    for the last method, the method signature clearly indicates that you intend for the method to return a String. yet the method only prints the numbers and does not return anything. there are 2 solutions, the first is just to change the return type to void. the second solution is to return
    number + ""; which automatically turns it into a string. you can then print this in another method.

  3. #3
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Missing return statement

    Quote Originally Posted by tomejuan View Post
    remember that all class names should be capitalized!
    change the class name to Marc1

    for the last method, the method signature clearly indicates that you intend for the method to return a String. yet the method only prints the numbers and does not return anything. there are 2 solutions, the first is just to change the return type to void. the second solution is to return
    number + ""; which automatically turns it into a string. you can then print this in another method.
    Class names DON'T have to be capitalized. Its just a good coding convention that you do. I was corrected on this fact earlier this week. Also, tomejuan is correct in saying that the reason you have a "missing return error" is due to the fact that your final method is setup to return a String type but you are only printing to the screen. So to correct this, change "String" to "void" in the method's header.
    while(true)
    {
            codeInJava();
    }

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Missing return statement

    Thanks for helping me get the return statement taken care of. All errors went away. Now when I went to run the code in my terminal. No matter what I enter, it continuously loops my system.out.print.

  5. #5
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Missing return statement

    I modified your main code a bit to reflect what I think you wanted to do. Firstly you seem to exit the program if the user enters a zero but there is no notice to the user about this. Secondly I'm presuming since you have the main program in a while loop, you want the program to run so long as zero is not entered. Finally, your validateLength(), I am taking it that you are looking for input of no more than one (ie the number entered is to be a single digit) based on the "You're number is too long" message. So I changed the conditional bracket in the method to check if the length was "Greater Than" 1 and not "Less Than" as you have it.
        System.out.println("What is your single digit integer?");
    	System.out.print("Type 0 to exit: "); //Tell the user how to exit program
    	int number= input.nextInt(); 
     
        while (number > 0) { //While the number entered is not 0
     
    		if (validateLength(number)){ //Validate number length
    			convertIntegerToWords(number); //if true, convert to word
    		}
     
    		System.out.print("Enter another single digit integer?: "); //request another integer
    		number = input.nextInt(); //store the new number and return to start of loop
       }   
     
       System.out.print( "Thank you for playing! Good bye!"); //User has decided to exit so print goodbye message before program ends
    }

    	if (userNumber.length() > 1) { //change made here!
    	System.out.print("The number you entered is too long!");
    	  return false;
            }
     
    	else
    	  return true;
          }

    Hope this helps a bit. If I have mistaken your code's functionality just make the necessary amends to facilitate larger numbers.
    while(true)
    {
            codeInJava();
    }

Similar Threads

  1. Missing return statement
    By Stn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2011, 08:03 PM
  2. what is wrong with my return statement??????
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 13th, 2010, 07:55 PM
  3. Missing Return Statement in If-elseIf-else
    By chronoz13 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 12th, 2009, 08:01 AM
  4. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 10:17 AM
  5. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 01:00 PM