How can I convert this into Java(Android) code?
a = input()
asplit = a.split()
mapy = map(int, asplit)
suma = sum(mapy)
I've tried this:
EditText marks;
String value= marks.getText().toString();
String[] myList = value.split(" ");
int marksfinal=Integer.parseInt();
but it doesn't work I want to convert the user input which will be digits, into an array of digits, them calculate the sum of the array.
1 Answer 1
You just need a loop to sum all the values in the array.
int marksfinal=0;
for(String s : myList)
marksfinal += Integer.parseInt(s);
Also if you want to split on arbitratry width for whitespaces use :
String[] myList = value.split("\\s+");
answered Jan 19, 2014 at 9:19
Alexis C.
94.3k22 gold badges173 silver badges179 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
42 38 -50, result is 30.int marksfinal=0; for(String s : myList) marksFinal += Integer.parseInt(s);