-2

When I attempt to call the daysInaMonth method in my main method, I get this error:

DaysInMonth.java:34: error: missing return statement
   }
   ^
1 error

What am I doing incorrectly? Why isn't the method call working? I've made sure that both methods return the correct data type. Here is my class file:

import java.util.Scanner;  
public class DaysInMonth { 

   public String daysInaMonth (int year_number, String month_name) {
      if(year_number % 4 != 0 || month_name != "February") {
         switch(month_name) {
            case "January":
            case "March":
            case "May":
            case "July":
            case "August":
            case "October":
            case "December":
                System.out.print("31");
               break;
            case "April":
            case "June":
            case "September":
            case "November":
                System.out.print("30");
               break;
            case "February":
               System.out.print("28");
               break;
            default:
               System.out.print("Please input ");
               break;
         }  
      }
      else {
           System.out.print("29");
      }

   }


   public String main(String[] args) {
   Scanner scnr = new Scanner(System.in);
   String month = " ";
   int year = 0;

   month = scnr.nextLine();

   year = scnr.nextInt();

   return daysInaMonth(year, month);


   }


}
Darien Springer
  • 467
  • 8
  • 17
  • 1
    -: public String daysInaMonth (int year_number, String month_name) this right here is hungry for string value. – Tushar Sharma Mar 18 '17 at 03:37
  • The answer here: this is super basic stuff. Turn to your learning material and study! It also very much helps to Google for your error message. – GhostCat Mar 18 '17 at 03:44
  • I'm voting to close this question as off-topic because it is about a basic, simple error that could be easily resolved by doing just a little bit of prior research. – GhostCat Mar 18 '17 at 03:45
  • Just put the words "java missing return statement" into Google... And the fact that other not so great questions see upvotes... Yes. That happens. But doesn't change anything. – GhostCat Mar 18 '17 at 04:31

2 Answers2

4

The problem is that your daysInaMonth() method doesn't return anything. The signature says it should return a String.

However, the method is currently setup to do printing, not to return Strings.

First Option: Keep It as a Printing Method

So, the easiest would be to change the signature of your method to:

public void printDaysInAMonth (int year_number, String month_name) {
     // ... your code ...
}

Then, in main(), instead of doing:

return System.out.print(daysInAMonth(year, month));

Just do:

printDaysInAMonth(year, month);

Second Option: Return Something

In daysInaMonth(), change every reference to System.out.print() to a return statement, to return the String. Also remove break, which will no longer be useful since you are returning. Example:

        case "December":
            return "31";

Now in main(), you can call the daysInaMonth() and it will return the String value:

String stringThatWeGotBack = daysInaMonth(year, month);
System.out.println(stringThatWeGotBack);

Or just:

System.out.println(daysInaMonth(year, month));
Jameson
  • 6,400
  • 6
  • 32
  • 53
1

Replace System.out.print to return statement inside daysInaMonth method.

Also, change return daysInaMonth(year, month); to System.out.print(daysInaMonth(year, month)) in main method.

Ravi
  • 30,829
  • 42
  • 119
  • 173