java - Checking for decimal after 10 digits - substring(i,1) not working -
i have 2 problems:
x.substring(i,1)breaks intry/catchbecause of , possibly other things, can't method work. i'm trying have when user enters decimal, increase
max lengthofedittext, decreasemax lengthoriginalmax lengthif user enters number large.boolean toobig = false; edittext txt = (edittext)findviewbyid(r.id.num1); textview display = (textview)findviewbyid(r.id.display); string x = txt.gettext().tostring(); // string in edittext string str = "didn't work"; try { if (x.contains(".")) { // set max length 15 if (x.length() >= 10) { // see if decimal contained after 10 digits (int = 10; < x.length(); i++) { str = x.subtring(i,1); // breaks here (test) if (x.substring(i,1).equals(".")) // breaks here { toobig = true; } } } if (toobig) { // set text blank , max length 8 } } } catch (exception e) { txt.settext(str); // "didn't work" }
what can fix x.substring(i,1) problem?
you have change (i,1) (i, i+1) because in java, second number index end point, not "how many characters go for", in other languages. start @ i, , end @ 1 more i.
you use indexof(".") solve problem instead. also, have else statement change max length original max length in case user erases decimal.
change try this:
if (x.contains(".")) { // set max length 15 if (x.indexof(".") > 9) { txt.settext(x.substring(0,9)); // set length 9 } } else { // set max length original }
Comments
Post a Comment