Hello, new to Java and I've been doing some coding for a few weeks. Right now I am stumped. I've been trying to make a program that would display how many days are in the month when the user inputs a month number, but it keeps saying that
DaysInMonth.java:64: error: variable days might not have been initialized
JOptionPane.showMessageDialog(null,days);
Whenever I set days to a like a placeholder number like 1, I get one back. Any ideas on how to fix this?
import javax.swing.JOptionPane;
public class DaysInMonth {
public static void main(String[]args) {
String inputString = JOptionPane.showInputDialog("Input your month number");
int month = inputString.charAt(0);
inputString = JOptionPane.showInputDialog("Input your year number");
int year = inputString.charAt(0);
int days;
switch (month) {
case 1: days = 31;
break;
case 2:
if (year % 4 == 0)
days = 28;
else days = 29;
break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 31;
break;
case 6: days = 30;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 30;
break;
case 11: days = 31;
break;
case 12: days = 31;
}
JOptionPane.showMessageDialog(null,days);
}
}