For the past 5 or so hours I've been trying to figure out how to condense this basic code that uses nextInt(); to read the first user input integer.
Code:
import java.util.Scanner;
public class DateCalculator {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int month;
int day;
int year;
int[] dayCount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("Enter integers for month, day, and year using 4 digits for the year: ");
month = s.nextInt();
if (s.equals("1")){
month = dayCount[0];
}
else if (s.equals("2")){
month = dayCount[1];
}
else if (s.equals("3")){
month = dayCount[2];
}
else if (s.equals("4")){
month = dayCount[3];
}
else if (s.equals("5")){
month = dayCount[4];
}
else if (s.equals("6")){
month = dayCount[5];
}
else if (s.equals("7")){
month = dayCount[6];
}
else if (s.equals("8")){
month = dayCount[7];
}
else if (s.equals("9")){
month = dayCount[8];
}
else if (s.equals("10")){
month = dayCount[9];
}
else if (s.equals("11")){
month = dayCount[10];
}
else if (s.equals("12")){
month = dayCount[11];
}
day = s.nextInt();
year = s.nextInt();
The above code is what I would like to make short by using a 'for' / 'while' loop (nested) or by using the 'switch' statements.........
........This code below is just me testing for potential ways to accomplish this....
Code:
//Displays element value
if (){
day = dayCount[2];
for (int d=0; d<dayCount.length; d++){
}
}
else{
for (int item : dayCount) {
System.out.println(item);
System.out.println(day);
}
}
Then this is how I will display error messages if the user inputs the wrong integers ( I used the 'else if' statement so that if there is more than one error the program will only display one error message.I want the number the user inputs (1-12)--1 being January, 2 being February, etc.-- to be assigned a variable so that later on the program will know which how to differentiate between elements .........
......
but you do it after collecting input (at least that's what it looks like from the code you posted)
whole code.....
Code:
import java.util.Scanner;
public class DateCalculator {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int month;
int day;
int year;
int[] dayCount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("Enter integers for month, day, and year using 4 digits for the year: ");
int j = s.nextInt();
if(j >= 1 && j <= 12) {
month = dayCount[j-1];
}
else{
month = j;
}
day = s.nextInt();
year = s.nextInt();
/*
//Displays element value; just an idea
if (){
day = dayCount[2];
for (int d=0; d<dayCount.length; d++){
}
}
else{
for (int item : dayCount) {
System.out.println(item);
System.out.println(day);
}
}
*///////////////
if (year < 1582){
System.out.println("Invalid Year");
}
else if (month < 0 || month >12 ){
System.out.println("Invalid Month");
}
else if(day < 0 || day >31){
System.out.println("Invalid Day");
}
System.out.println("Month is: " + month);
if (year % 4 == 0) {
// divisible by 4 but not 100?
if (year % 100 != 0) {
System.out.println(year + " is a leap year.");
}
//divisible by 4 and 100 and 400?
else if (year % 400 == 0) {
System.out.println(year + " is a leap year.");
}
// It is divisible by 4 and 100 but not 400
else {
System.out.println(year + " is not a leap year.");
}
}
// It is not divisible by 4.
else {
System.out.println(year + " is not a leap year.");
}
}
}
Bookmarks