I have had some problems with one of my codes lately and now that I managed to get it working it crashes every time I run it. The problem is that when I debug step by step the app works but when I take off the steps it crashes every time...
This is the code:
String r = users.getListRatings();
userHolder.tx_listratings.setText(" / " + getAverage(r) + " \uD83C\uDF1F"); //crashes here: at com.example.juanpablo.coffee.UserListAdapter.getView(UserListAdapter.java:86)
public int getAverage(String args) {
String a = args;
String[] x = a.split("");
int add = 0;
for (int i = 1; i < x.length; i++) {
add = add + Integer.parseInt(x[i]); }
int average = add / a.length(); //crashes here: at com.example.juanpablo.coffee.UserListAdapter.getAverage(UserListAdapter.java:114)
return average;
}
This is the crash: java.lang.ArithmeticException: divide by zero.
Please help me if you can and if downvote please explain why, I am new and need to improve. Thank you.
我通过将 a.length() 更改为 i 来解决此问题。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.