So I started the code below to make a date printer, I screwed up that code so I decided to try to change all of the if and else statements to switch statements. The code is suppose to take two integers and print out a date. For example typing in: 1 2 would print: January 2nd
I know the code I have below is not correct, but it will probably help explain what I am trying to do. (Sorry I am pretty new to this)
class DatePrinter {
public static void main(String[] args) {
int month, day;
boolean dateIsValid = false;
System.out.println("Enter two integers. Press return after each
integer.");
month = A7Helper.nextInteger();
day = A7Helper.nextInteger();
if (month == 4 && day >= 1 && day <= 30) {
System.out.print("April ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 5 && day >= 1 && day <= 31) {
System.out.print("May ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 7 && day >= 1 && day <= 30) {
System.out.print("June ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 7 && day >= 1 && day <= 31) {
System.out.print("July ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 8 && day >= 1 && day <= 31) {
System.out.print("August ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 9 && day >= 1 && day <= 31) {
System.out.print("September ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 10 && day >= 1 && day <= 31) {
System.out.print("October ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 11 && day >= 1 && day <= 30) {
System.out.print("November ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 12 && day >= 1 && day <= 31) {
System.out.print("December ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 1 && day >= 1 && day <= 31) {
System.out.print("January");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 2 && day >= 1 && day <= 28) {
System.out.print("February ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (month == 3 && day >= 1 && day <= 31) {
System.out.print("March ");
System.out.print(day);
dateIsValid = true;
} else {
System.out.println("Invalid Date");
dateIsValid = false;
}
if (day == 1) {
System.out.println("th");
} else {
System.out.println("th");
}
if (day == 2) {
System.out.println("nd");
} else {
System.out.println("th");
}
if (day == 3) {
System.out.println("rd");
} else {
System.out.println("th");
}
}
}
also the A7Helper
import java.util.Scanner;
public class A7Helper {
static Scanner keyboard = new Scanner(System.in);
static int nextInteger() {
return keyboard.nextInt();
}
}