Check if a String is a double or an int?
Posted
by user69514
on Stack Overflow
See other posts from Stack Overflow
or by user69514
Published on 2010-05-07T00:21:46Z
Indexed on
2010/05/07
0:28 UTC
Read the original article
Hit count: 596
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
}
© Stack Overflow or respective owner