I want to take year and month from the user one per line and I have to print the day of week for the corresponding year and month with day as 28 for all inputs, i printed the day_of_week for the first input and for second input an exception is thrown by jvm can anyone help me??
For example the inputs are:
The console should be able to display the:
The month starts from 0 to 11(0-11)
import java.util.Calendar;
public class Calendar1 {
public static void main(String[] args) {
int day = 0;
String[] input = new String[2];
int[] year = new int[1];
int[] month = new int[1];
String[] split = new String[2];
Calendar cal = Calendar.getInstance();
System.out.println("Enter The Year And Month(YYYY-M): ");
Scanner s = new Scanner(System.in);
for(int i=0;i<2;i++) {
input[i] = s.nextLine();
}
for(int i=0;i<2;i++) {
split = input[i].split("-");
year[i] = Integer.parseInt(split[0]);
month[i] = Integer.parseInt(split[1]);
cal.set(year[i],month[i],28);
day[i] = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(day[i]);
}
}
}
You array bounds seem inappropriate. Probably change your them from
int day = 0;
int[] year = new int[1]; // array containing only one element
int[] month = new int[1];
to
int[] day = new int[N];
int[] year = new int[N];
int[] month = new int[N]; // where N should be the number of lines of your input (2 for the above use case)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.