snippetjavaMinor
Converting numeric (month/day/year) date inputs to regular format (month day, year)
Viewed 0 times
inputsformatyearnumericregulardatemonthconvertingday
Problem
I'd really appreciate some general formatting/style/performance tips, as well as any ways I could improve this code in general that I wrote for an assignment (title) that's due at the end of the week. The code passes every test input given (leap years and such), so the logic is all set. I had the
```
import java.util.*;
class Dates {
private static final Set THIRTY_DAY_MONTHS = new HashSet<>(Arrays.asList(4, 6, 8, 11));
private String monthString;
private Dates() {
//Initializing input method
System.out.println("Enter month/day/year to be converted to normal format: ");
Scanner s = new Scanner(System.in);
//Creates an array of strings split at forward slash
String[] input = s.nextLine().split("/");
//Converts the array of strings to array of ints with matching indices
int[] inputNumbers = new int[input.length];
for (int i = 0; i 12 || inputNumbers[0] 30) { //Logic for valid day ranges for months that only contain 30 days
throw new DayException();
} else if (inputNumbers[1] > 31 || inputNumbers[1] 29 && inputNumbers[2] % 4 == 0 && (inputNumbers[2] % 100 != 0 || inputNumbers[2] % 400 == 0)) { //Logic for # of days in February for a leap year
throw new DayException();
} else if (inputNumbers[2] 3000) { //Logic for valid range of years
throw new YearException();
}
//Prints and formats the numeric input as a string
System.out.println(monthString + ' ' + inputNumbers[1] + ", " + inputNumbers[2]);
} catch (MonthException e) {
System.out.println(e.getMessage());
loop();
} catch (DayException e) {
System.out.println(e.getMessage());
loop();
} catch (YearExcepti
HashSet as a List originally, but I read that a HashSet will give better performance, although I'm not sure if it even matters with only 4 values.```
import java.util.*;
class Dates {
private static final Set THIRTY_DAY_MONTHS = new HashSet<>(Arrays.asList(4, 6, 8, 11));
private String monthString;
private Dates() {
//Initializing input method
System.out.println("Enter month/day/year to be converted to normal format: ");
Scanner s = new Scanner(System.in);
//Creates an array of strings split at forward slash
String[] input = s.nextLine().split("/");
//Converts the array of strings to array of ints with matching indices
int[] inputNumbers = new int[input.length];
for (int i = 0; i 12 || inputNumbers[0] 30) { //Logic for valid day ranges for months that only contain 30 days
throw new DayException();
} else if (inputNumbers[1] > 31 || inputNumbers[1] 29 && inputNumbers[2] % 4 == 0 && (inputNumbers[2] % 100 != 0 || inputNumbers[2] % 400 == 0)) { //Logic for # of days in February for a leap year
throw new DayException();
} else if (inputNumbers[2] 3000) { //Logic for valid range of years
throw new YearException();
}
//Prints and formats the numeric input as a string
System.out.println(monthString + ' ' + inputNumbers[1] + ", " + inputNumbers[2]);
} catch (MonthException e) {
System.out.println(e.getMessage());
loop();
} catch (DayException e) {
System.out.println(e.getMessage());
loop();
} catch (YearExcepti
Solution
In addition to h.j.k.'s comments....
There is a common trick you can play when dealing with data that is referenced back to a sequential number, like months. If you create an array of Month names, and an array of days in each month, like:
Then, when the person enters a month, you can subtract 1, and index it back in to the above array, and get rid of all the if's:
Note how there is no need for the days-in-month list/set at all?
Also, note how I rename the 'inputValues' members to names that make sense.
There is a common trick you can play when dealing with data that is referenced back to a sequential number, like months. If you create an array of Month names, and an array of days in each month, like:
private static final String[] MONTH_NAMES = {"January", "February", ".....", ...};
private static final int[] MONTH_DAYS = {31,29,31,30,31,30,31,31,30,31,30,31};Then, when the person enters a month, you can subtract 1, and index it back in to the above array, and get rid of all the if's:
int monthIndex = inputNumbers[0] - 1;
int monthDay = inputNumbers[1];
int year = inputNumbers[2];
if (monthIndex = MONTH_NAMES.length) {
throw new IllegalArgumentException("....");
}
int maxDays MONTH_DAYS[monthIndex];
String monthString = MONTH_NAMES[monthIndex];
if (monthDay maxDays
|| monthIndex == 1 && monthDay == 29 && (year % 4 != 0 || year % 100 == 0)) {
throw new ....
}Note how there is no need for the days-in-month list/set at all?
Also, note how I rename the 'inputValues' members to names that make sense.
Code Snippets
private static final String[] MONTH_NAMES = {"January", "February", ".....", ...};
private static final int[] MONTH_DAYS = {31,29,31,30,31,30,31,31,30,31,30,31};int monthIndex = inputNumbers[0] - 1;
int monthDay = inputNumbers[1];
int year = inputNumbers[2];
if (monthIndex < 0 || monthIndex >= MONTH_NAMES.length) {
throw new IllegalArgumentException("....");
}
int maxDays MONTH_DAYS[monthIndex];
String monthString = MONTH_NAMES[monthIndex];
if (monthDay < 1 || monthDay > maxDays
|| monthIndex == 1 && monthDay == 29 && (year % 4 != 0 || year % 100 == 0)) {
throw new ....
}Context
StackExchange Code Review Q#63529, answer score: 3
Revisions (0)
No revisions yet.