user3211760
user3211760

Reputation: 41

Convert this from Python into Java

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.

Upvotes: 2

Views: 141

Answers (1)

Alexis C.
Alexis C.

Reputation: 93842

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+");

Upvotes: 3

Related Questions