0

I am new to java dev. I want to add numbers in an editText. i.e if user types in 15 in editText, it should add the numbers 1 + 5 giving the result 6. Is there a function for it in java. In C# it is ToCharArray() but I don't know what it's called in java. Thanks

2 Answers 2

1

you can use

    String str = "15";
    char[] cArray = str.toCharArray();

    int sum = 0;
    for (char c : cArray)
        sum += Character.digit(c, 10);
Sign up to request clarification or add additional context in comments.

6 Comments

I am using the above code, but the app seems to crash.Also I am getting error (cannot parse int here) when I try to use sum += Integer.parseInt(c) Any help will be great.
Also how can i put the result in a textview. I am using textv.setText(sum) but it doesn't seems to be working.
@artist try my edit for the error. As far as the textView, textv.setText(sum.toString()) should work.
still says that application has stopped unexpectedly.
It works now. I want to know what does 10 stands for in the above code sum += Character.digit(c, 10); Thanks
|
1

When translating between C# and Java, you can get very far by doing nothing more than changing the capitalization. The equivalent method in Java's String class is "toCharArray()", starting with a lower-case "t".

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.