2

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.

4
  • Summary of Python code: take user input, split it on whitespace (arbitrary width), convert the resulting list of strings into integers and sum those integers. Input 42 38 -50, result is 30. Commented Jan 19, 2014 at 9:11
  • 2
    You just need a loop to sum all the values in the array. int marksfinal=0; for(String s : myList) marksFinal += Integer.parseInt(s); Commented Jan 19, 2014 at 9:13
  • 2
    @ZouZou make that an answer, no? Commented Jan 19, 2014 at 9:16
  • +1 to the Q for not being "Please do this for me even though I haven't tried" Commented Jan 19, 2014 at 9:42

1 Answer 1

3

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+");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.