static boolean isNumber(String number){
if(number.charAt(0)=='-'){
for(int i = 1 ; i < number.length() ; i++){
if( Character.isDigit(number.charAt(i))){
}
else{
return false;
}
}
}
else{
for(int i = 0 ; i < number.length() ; i++){
if( Character.isDigit(number.charAt(i))){
}
else{
return false;
}
}
}
return true;
}
Well the above code can be simplified alot with the use of ternary operator
static boolean isNumber(String number){
for(int i = number.charAt(0)=='-' ? 1: 0 ; i < number.length() ; i++){
if( Character.isDigit(number.charAt(i))){
}
else{
return false;
}
}
return true;
}
No comments:
Post a Comment