I have textfield array which has to accept only numeric values i.e. 1-5.
<s:textfield name="marks[0]" maxlength="1" />
<s:textfield name="marks[1]" maxlength="1" />
<s:textfield name="marks[2]" maxlength="1" /> and so on upto 9 textfields.
I have getter/setter methods for this as follows
public List<Integer> getMarks(){
return marks;
}
public void setMarks(List<Integer> marks){
this.marks = marks;
}
How to ensure that user does not enter non numeric values like alphabets? If I enter character it throws runtime exception 'java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer ' pointing to validate method in the Next JSp page.
public void validate(){
for(i=0;i<9;i++){
if (marks.get(i)>5) //this line throws above ClassCastException
addFieldError("...");
}
}
How to solve this exception as well as validation to continue? In other Pages where single textfield is there, invalid.fieldvalue.error is displayed but for List I am getting above exception