java - method to validate that amount should be of type integer return type -


i getting amount in object through getter method of pojo amount getter method return type set string in pojo shown below

//setting need done in pojo private string amount;      public string getamount() {         return amount;     } 

let below there object h , retrieving

h.getamount() 

now need develop validator validate amount should of type integer , if not throw exception please advise how can develop seprate method check whether amount in integer or not , on basis of return true or false , shown below

// validate amount in integer private boolean isvalidamount (string amount) {     boolean valid = false; //code check whether amount integer or not, if integer //return true else return false } 

i have updated post throws number format exception , please advise

you try parse it, , return true iff parse succeeds.

try {     integer.parseint(amount);     return true; } catch (numberformatexception e) {     return false; } 

edit

i re-read question , noticed seems thing want true/false value possibly raise exception if string couldn't parsed. in case, can rid of boolean middleman:

try {     integer.parseint(amount); } catch (numberformatexception e) {     throw new mywhateverexception(amount); } 

Comments