Check if a String is a double or an int?
- by user69514
I have a string for a Date in the form mm/dd, and I need to check if either the month or day was entered as a double
public Date(String dateStr){
int slash = 0;
//check slash is present
try{
slash = dateStr.indexOf('/');
}catch(StringIndexOutOfBoundsException e){
error = "Invalid date format: " + dateStr;
}
//check if month is a number
try{
month = Integer.parseInt(dateStr.substring(0, slash));
//day = Integer.parseInt(dateStr.substring(slash + 1, dateStr.length()));
}
catch(NumberFormatException e){
System.out.println("Invalid format for input string: " + dateStr.substring(0, slash));
}
//check if day is a number
try{
day = Integer.parseInt(dateStr.substring(slash + 1, dateStr.length()));
}
catch(NumberFormatException e){
System.out.println("Invalid format for input string: " + dateStr.substring(slash + 1, dateStr.length()));
}
//check if month was entered as a double
}