I'm teaching myself java and I came across this error. I'm trying to enter a number of a month and I have to use a switch case to return the "answer". I get the follow error:
package javauebung5;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Type Number for name of month");
int monthNumber = scanner.nextInt();
monthNumber = determineNameofMonth(monthNumber);
}
private static String determineNameofMonth(int monthNumber) {
switch (monthNumber) {
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
}
return "Invalid Month";
}
}
This is the error:
Incompatible types. Found: 'java.lang.String', required: 'int'
Per its declaration and intent, determineNameofMonth returns a String. Here you attempt to assign to an integer; the error message is telling you that an integer variable requires an integer value.
Use something like:
(I assume you'll then want to write code to print out monthName, otherwise you're doing nothing with the result of the computation).